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

时间:2018-03-09 01:08:50

标签: 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 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()

1 个答案:

答案 0 :(得分:0)

问题在于:

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(…),并将结果作为command的{​​{1}}传递。

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

然后,Button的结果不是函数,因此您存储为execute的内容不是可以调用的内容,因此稍后单击该按钮不会执行任何操作,只需写入警告即可command,你不是在看。

你需要创建一个函数(没有参数),你可以传递给stderr - 这个函数在被调用时会为你做这个插入。例如:

command

如果您希望在一个巨大的表达式中完成所有操作,就像在旧代码中一样,您可以使用def on_submit(): cur.execute('INSERT INTO record blah blah blah' …, value_text.get())) submit = Button(window, text='submit', command=on_submit) 代替lambda,或使用def,但是它可能会更加清晰。

相关问题