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.
try/exceptand you will see error message with useful information (instead of useless"Except"). Or at least useexcept Exception as ex: print(ex).executeexpect?instead of(%s)in query.(in_val)is not tuple, it is single element, To create tuple you need comma inside(in_val, )