1

I am trying to establish connection with MySQL database using Python on RaspberryPi. I am new to object oriented python. In this code I am taking a value as input and trying to insert it in the database. Due to some issue, The insert query is not getting executed and the Exception part is getting executed. Please help me in identifying my mistake.

import tkinter as tk
import MySQLdb   

class ImgComp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)        
        container = tk.Frame(self,width=320, height=209)        
        container.pack(side="top")
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        container.grid_propagate(False)

        try:
            self.db = MySQLdb.connect("localhost","root","root","sample")
            self.c= self.db.cursor()                
        except:
            print ("I can't connect to MySql")
        self.frames = {}
        for F in (InputPage):            
            frame = F(container, self)                
            self.frames[F] = frame            
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(InputPage)   

    def show_frame(self, cont):
        frame = self.frames[cont]                    
        if ((cont == PageOne) | (cont == InputPage) | (cont == OutputPage)):            
            self['menu'] = frame.menubar                
        else: 
            self['menu'] = ''            
        frame.tkraise()

    def input_submit(self, input_value):            

        in_val = float(input_value)
        self.sql = "INSERT INTO input_delay_master (input_delay) VALUES (%s)"                
        try:                   
            c.execute(self.sql,(in_val))                    
            self.db.commit()
        except:                    
            print("Except")
            self.db.rollback()         

class InputPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.menubar = tk.Menu()
        filemenu = tk.Menu(self.menubar, tearoff=0)            
        filemenu.add_command(label="Output Reset Delay", command=lambda: controller.show_frame(OutputPage))                        
        self.menubar.add_cascade(label="Settings", menu=filemenu)        
        label = tk.Label(self, text="Input Image Delay")
        label.pack(pady=10,padx=10)                       
        input_delay = tk.Entry(self)
        input_delay.pack(pady=10)
        submit1 = tk.Button(self, text = "Submit", command=lambda: controller.input_submit(input_delay.get()))
        submit1.pack() 

I get the following output

34.56 # This is the the value I entered in Entry field    

Except     

The problem is, the insert query present inside the input_submit button is not getting executed, rather Expection part is executed. Please help me getting the way out to successfully execute the insert query.

10
  • Try print inside try to debug Commented Jan 2, 2018 at 10:55
  • I tried it, it is not getting executed, directly except part is getting executed Commented Jan 2, 2018 at 10:56
  • for testing code remove try/except and you will see error message with useful information (instead of useless "Except"). Or at least use except Exception as ex: print(ex). Commented Jan 2, 2018 at 11:44
  • I'm not sure but probably execute expect ? instead of (%s) in query. Commented Jan 2, 2018 at 11:49
  • 1
    except may expect tuple or list with arguments (even if you have only one argument) - but (in_val) is not tuple, it is single element, To create tuple you need comma inside (in_val, ) Commented Jan 2, 2018 at 11:54

1 Answer 1

2

except expects tuple with arguments - even if you have only one argument - but (in_val) is not tuple, it is single element. To create tuple you need comma inside (in_val, )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.