Python 如何使用insert语句将输入小部件中的文本保存到数据库中?

Python 如何使用insert语句将输入小部件中的文本保存到数据库中?,python,tkinter,Python,Tkinter,我试图从输入字段中获取数据并将其保存在数据库中。我在SQL查询中提到了6列,但在数据库中还有一列是ID,它是自动递增的。当我运行这个脚本时,我没有错误,但是也没有数据被插入到数据库中,而不是自动递增的ID中。这是我的密码: window=tkinter.Tk() # Set the window title window.title("Princedeep Singh") window.geometry("500x400") current_row = 0 # Set the current

我试图从输入字段中获取数据并将其保存在数据库中。我在SQL查询中提到了6列,但在数据库中还有一列是ID,它是自动递增的。当我运行这个脚本时,我没有错误,但是也没有数据被插入到数据库中,而不是自动递增的ID中。这是我的密码:

window=tkinter.Tk()
# Set the window title
window.title("Princedeep Singh")
window.geometry("500x400")
current_row = 0

# Set the current row to zero

refdate_label = Label(window, text="Enter value for date: ")
refdate_label.grid(row=current_row, column=0)
refdate_text = StringVar()
refdate_entry = Entry(window, textvariable=refdate_text)
refdate_entry.grid(row=current_row, column=1)

current_row += 1

# geogeo field
geo_t_label = Label(window ,text="Enter value for geo:")
geo_t_label.grid(row=current_row, column=0)
geo_t_text = StringVar()
geo_t_entry = Entry(window, textvariable=geo_t_text)
geo_t_entry.grid(row=current_row, column=1)
current_row += 1


# commod field
commod_label = Label(window, text="Enter value for commod:")
commod_label.grid(row=current_row, column=0)
commod_text = StringVar()
commod_entry = Entry(window, textvariable=commod_text)
commod_entry.grid(row=current_row, column=1)
current_row += 1


# vector
vector_label = Label(window, text="Enter value for vector:")
vector_label.grid(row=current_row, column=0)
vector_text = StringVar() 
vector_entry = Entry(window, textvariable=vector_text)
vector_entry.grid(row=current_row, column=1)
current_row += 1



# commod field
coordinate_label = Label(window, text="Enter value for coordinate:")
coordinate_label.grid(row=current_row, column=0)
coordinate_text = StringVar()
coordinate_entry = Entry(window, textvariable=coordinate_text)
print(coordinate_text)
coordinate_entry.grid(row=current_row, column=1)
current_row += 1


value_label = Label(window, text="Enter value :")
value_label.grid(row=current_row, column=0)
value_text = StringVar()
value_entry = Entry(window, textvariable=value_text)
value_entry.grid(row=current_row, column=1)
current_row += 1



conn = mysql.connector.connect(host='localhost', db='postgres', user='root', 
       password='root')
cur = conn.cursor()

submit = Button(window, text='submit', command=cur.execute('INSERT INTO 
         record(refdate,geo,commod,vector,coordinate,value) VALUES (%s, %s, 
         %s, %s, %s,%s)',
         (refdate_text.get(),geo_t_text.get(),commod_text.get()
         ,vector_text.get(),str(coordinate_text.get()),value_text.get() )))  
submit.grid(row=current_row+1, column=4)
conn.commit()
window.mainloop()
问题在于:

submit = Button(window, text='submit', command=cur.execute('INSERT INTO 
         record(refdate,geo,commod,vector,coordinate,value) VALUES (%s, %s, 
         %s, %s, %s,%s)',
         (refdate_text.get(),geo_t_text.get(),commod_text.get()
         ,vector_text.get(),str(coordinate_text.get()),value_text.get() )))  
submit.grid(row=current_row+1, column=4)
您正在创建时调用
cur.execute(…)
,并将结果作为
按钮的
命令传递给

当然,在创建时,条目都是空的,因此插入一个空行

然后,
execute
的结果不是一个函数,因此存储为
命令
的内容不能调用,因此稍后单击按钮除了向您没有看到的
stderr
写入警告之外,什么都不做

您需要创建一个函数(不含参数),可以传递给
命令
——调用该函数时,将为您执行此插入操作。例如:

def on_submit():
    cur.execute('INSERT INTO record blah blah blah' …, value_text.get()))

submit = Button(window, text='submit', command=on_submit)

如果您喜欢在一个巨大的表达式中完成这一切,就像在旧代码中一样,您可以使用
lambda
而不是
def
,或者使用
functools.partial(execute)
,但这样可能更清晰。

欢迎使用!请仔细阅读,并提供一份能再现您的问题的报告。感谢abarnert提供您的解决方案。我听了你的回答,但我有同样的问题。我在类下做了两个独立的函数,一个函数有上面列出的所有代码,另一个方法有db连接和查询。我将变量设置为全局变量,但不知道如何使用变量值从一个函数到另一个函数。请给我一些建议。@ZimidarBoy你所做的解释对我调试它来说还远远不够。如果在对代码进行了一个小的更改之后,您实际上遇到了相同的问题,请编辑该问题。如果在重写代码后遇到类似问题,请发布新问题。(你可以链接到这一个来解释你为什么这么做。)无论哪种方式,这都给了你空间来包含一个人们可以调试的完整示例,这意味着你的示例将被整个SO Python社区看到,而不仅仅是那些阅读旧答案下注释的人。