【问题标题】:Not able to update table using tkinter in mysql无法在 mysql 中使用 tkinter 更新表
【发布时间】:2021-11-21 05:10:46
【问题描述】:

我无法使用 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()

【问题讨论】:

    标签: python mysql tkinter updates pymysql


    【解决方案1】:

    这是因为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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      相关资源
      最近更新 更多