You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Tkinter更新MySQL数据表无报错但未生效问题求助

解决Tkinter操作MySQL无法更新数据表的问题

我帮你排查了代码里的几个关键问题,这就是为什么你的更新功能没效果的原因:


1. 更新按钮根本没被创建

你把apply按钮的创建代码放在了第一个t.mainloop()之后:

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

mainloop()会启动Tkinter的事件循环,程序会一直卡在这个循环里,后面的代码永远不会执行,所以这个更新按钮实际上不存在,你自然没法触发更新操作。

2. SQL更新语句的参数顺序完全颠倒了

在upd函数里,你写的SQL是:

update sample2 set age =%s where name = %s

这里第一个%s对应age,第二个对应name,但你传的参数是:

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

其中gnd = b.get()是用户输入的要修改的名字,anag = nag.get()是新的年龄,参数顺序搞反了,应该把年龄放在前面,名字放在后面:

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

3. 缺少异常捕获,无法排查潜在错误

现在的代码没有任何异常处理逻辑,就算执行过程中出了问题(比如数据库连接失败、数据类型不匹配),你也看不到任何报错信息,很难定位问题。建议给数据库操作加上try-except块。


修正后的完整代码

from tkinter import * 
import pymysql  # 把导入放在顶部,不要在函数里重复导入

t = Tk() 
t.geometry("500x500") 

# 插入部分的UI
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(): 
    try:
        x = pymysql.connect(host='localhost', user='root', password='admin', db='db') 
        cur = x.cursor() 
        n = nm.get() 
        a = ag.get()
        # 如果age是整数类型,这里要转成int:a = int(ag.get())
        cur.execute('insert into sample2 values(%s, %s)', (n, a)) 
        x.commit() 
        print("数据插入成功")
    except Exception as e:
        print(f"插入失败:{str(e)}")
        # 如果出错,回滚事务
        x.rollback()
    finally:
        # 确保连接关闭
        if 'x' in locals():
            x.close() 

Button(text='Submit', command=abcd).place(x=40,y=65) 

# 更新部分的UI
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) 

def upd(): 
    try:
        x = pymysql.connect(host='localhost', user='root', password='admin', db='db') 
        cur = x.cursor() 
        gnd = b.get() 
        anag = nag.get()
        # 如果age是整数类型,转成int:anag = int(nag.get())
        cur.execute('update sample2 set age =%s where name = %s', (anag, gnd)) 
        x.commit()
        print(f"成功更新{cur.rowcount}条数据")
        # 如果rowcount是0,说明没有匹配到要更新的记录
        if cur.rowcount == 0:
            print("没有找到对应的名字,请检查输入")
    except Exception as e:
        print(f"更新失败:{str(e)}")
        x.rollback()
    finally:
        if 'x' in locals():
            x.close() 

# 把按钮创建放在mainloop之前
Button(text='apply', command=upd).place(x=200, y=300) 

t.mainloop()

额外提示

  • 把pymysql的导入放在代码顶部,不要在函数里重复导入,这是更规范的写法。
  • 如果你的sample2表的age字段是整数类型,记得把输入框的内容转成int,否则会触发数据类型不匹配的错误。
  • 新增的print语句可以帮你在控制台看到操作结果,比如有没有更新到数据,或者有没有报错。

内容的提问来源于stack exchange,提问作者purple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 15:57:32