我正在尝试使用RaspberryPi上的Python建立与MySQL数据库的连接。我是第一次接触面向对象的python。在这段代码中,我接受一个值作为输入,并尝试将其插入到数据库中。由于某些问题,insert查询无法执行,而Exception
部件正在执行。请帮我找出我的错误。
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()
我得到了以下输出
34.56 # This is the the value I entered in Entry field
Except
问题是,没有执行input_submit
按钮中的插入查询,而是执行了Expection
部件。请帮助我找到成功执行insert查询的方法。
发布于 2018-01-02 20:20:18
except
需要带参数的tuple
-即使你只有一个参数-但(in_val)
不是元组,它是单个元素。要创建tuple
,您需要在(in_val, )
中使用逗号
https://stackoverflow.com/questions/48059459
复制相似问题