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

使用Python函数无法更新MySQL个人信息表的问题求助

问题分析与修复方案

核心问题排查

你的代码存在几个关键问题,导致数据未实际更新却弹出成功提示:

1. 字段验证错误

输入验证部分,self.var_HomeAddress.get和self.var_Occupation.get缺少调用括号(),会把方法对象和空字符串做比较,永远返回False。也就是说,即便这两个字段为空,也不会触发错误提示,直接进入更新逻辑。

错误代码:

or self.var_HomeAddress.get=="" or self.var_Occupation.get==""

修复后:

or self.var_HomeAddress.get()=="" or self.var_Occupation.get()==""

2. 更新条件无校验且可能不匹配

UPDATE语句使用Nationality作为WHERE条件,这通常不是唯一标识(多个用户可能国籍相同),如果self.var_radio2.get()的值与目标记录的国籍不一致,会导致0条记录被更新,但原代码没有检查更新行数,直接弹出成功提示。

另外,askyesno返回布尔值(True/False),用Update>0判断冗余,直接用if Update:即可。

3. 数据库操作逻辑顺序问题

原代码把conn.commit()放在成功提示之后,逻辑顺序不合理;同时若用户取消更新,conn资源未被正确处理(不过实际因为return不会走到后续代码,不会报错,但仍需规范)。

完整修复后的代码

#======= Update Function =================
def update_data(self):
    # 修复字段验证的.get()调用,格式化条件提升可读性
    if (self.var_Gender.get() == "Select Gender" 
        or self.var_FirstName.get() == "" 
        or self.var_LastName.get() == "" 
        or self.var_Nationality.get() == "" 
        or self.var_HomeAddress.get() == "" 
        or self.var_Occupation.get() == ""):
        messagebox.showerror("Error", "All fields are required", parent=self.root)
    else:
        try:
            update_confirm = messagebox.askyesno("Update", "Do you want to update this person's details", parent=self.root)
            if update_confirm:
                conn = mysql.connector.connect(host="localhost", username="root", password="tupsy@5050", database="details")
                my_cursor = conn.cursor()
                my_cursor.execute(
                    "update intel set Gender=%s,FirstName=%s,LastName=%s,Nationality=%s,Occupation=%s,HomeAddress=%s,PhoneNo=%s,Email=%s where Nationality=%s",
                    (
                        self.var_Gender.get(),
                        self.var_FirstName.get(),
                        self.var_LastName.get(),
                        self.var_Nationality.get(),
                        self.var_Occupation.get(),
                        self.var_HomeAddress.get(),
                        self.var_PhoneNo.get(),
                        self.var_Email.get(),
                        self.var_radio2.get()
                    )
                )
                # 检查是否有记录被更新,避免虚假成功提示
                if my_cursor.rowcount == 0:
                    messagebox.showwarning("Warning", "No matching record found to update", parent=self.root)
                else:
                    conn.commit()
                    messagebox.showinfo("Success", "Person's details successfully updated", parent=self.root)
                    self.fetch_data()
                conn.close()
            else:
                return

        except Exception as es:
            messagebox.showerror("Error", f"Due To:{str(es)}", parent=self.root)

额外建议

  • 尽量使用唯一标识字段(比如用户ID)作为WHERE条件,避免因国籍重复导致误更新多条记录或找不到目标记录。
  • 建议用with语句管理数据库连接和光标,自动处理资源释放,避免泄漏:
    with mysql.connector.connect(host="localhost", username="root", password="tupsy@5050", database="details") as conn:
        with conn.cursor() as my_cursor:
            my_cursor.execute(...)
            # 后续操作
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 22:35:09