无法在mysql中使用tkinter更新表



我无法使用Tkinter更新MySQL中的表。它不会更新。我试过检查表名不同的地方,但它不是。它显示没有错误,但没有更新。

from tkinter import *
t = Tk()
t.geometry("500x500")
Label(text = 'Name:').place(x=10,y=10)
nm = Entry()
nm.place(x=55,y=12)
Label(text = 'age:').place(x=10,y=35)
ag = Entry()
ag.place(x=55,y=37)

def abcd():
import pymysql
x = pymysql.connect(host = 'localhost',user = 'root',password = 'admin',db ='db')
cur = x.cursor()
n= nm.get()
a = ag.get()
cur.execute('insert into sample2 values(%s, %s)',(n,a))
x.commit()
x.close()



Button(text ='Submit',command = abcd).place(x=40,y=65)
Label(text ='UPDATE',fg = 'white',bg = 'black',font = ('Times new roman',24,'bold')).place(x=10,y=100)
Label(text ='Enter the name to update').place(x = 5,y = 155)
b=Entry()
b.place(x = 150, y = 157)
Label(text = 'Enter new age:').place(x=5,y = 200)
nag = Entry()
nag.place(x=150,y = 202)
'''print(b)'''
def upd():
import pymysql
x = pymysql.connect(host = 'localhost',user = 'root',password = 'admin',db ='db')
cur = x.cursor()
gnd =b.get()
anag = nag.get()
cur.execute('update sample2 set age =%s  where name = %s',(gnd,anag))
x.commit()
x.close()
t.mainloop()

Button(text = 'apply',command = upd).place(x = 200, y = 300)

t.mainloop()

这是因为UPDATE中使用的参数顺序错误:

cur.execute('update sample2 set age =%s  where name = %s',(gnd,anag)) # wrong order of arguments

WHERE子句被计算为False,并且不会更新任何记录。

应该是:

cur.execute('update sample2 set age =%s  where name = %s',(anag, gnd))

最新更新