Python 不更新表

标签 python mysql tkinter

我正在尝试使用 Python 制作数据库 GUI 应用程序,但无法更新表。没有错误,但我在查看 Mysql 工作台后运行程序没有任何变化。

class DB:
    def __init__(self):
        self.conn = mysql.connector.connect(host='localhost',                                                   database='databese', user='user',                                              
         password='password')
        self.cur = self.conn.cursor()
        self.conn.commit()

    def update(self, isim):
        self.conn.commit()
        self.cur.execute("UPDATE hakemler SET durum='UYGUNSUZ' WHERE isim='%s' ",isim)

    def updateback(self, isim):
        self.cur.execute("UPDATE hakemler SET durum='UYGUN' WHERE isim='%s'",isim)
        self.conn.commit()
db = DB()

def çıkar():
    db.update(id.get())
def gerial():
    db.updateback(hakem1.get())
hakem = StringVar()
hakem1= StringVar()
id  = IntVar()
entry1 = tk.Entry(mazeretli, width=20, textvariable=id)
entry1.grid(row=1, column=1)
button1 = tk.Button(mazeretli, text="Kişiyi Listeden Çıkar", command=çıkar)
button1.grid(row=2, column=2)
entry2 = tk.Entry(mazeretli, width=20, textvariable=hakem1)
entry2.grid(row=3, column=1)
button2 = tk.Button(mazeretli, text="Kişiyi Geri Al", command=gerial)
button2.grid(row=4, column=2)

没有错误信息或者其他什么。代码根本不起作用

最佳答案

您的问题在这里:

def update(self, isim):
    self.conn.commit()
    self.cur.execute("UPDATE hakemler SET durum='UYGUNSUZ' WHERE isim='%s' ",isim)

数据库操作要求您在操作后提交,以便将更改写入数据库。你把顺序颠倒了。更换顺序:

def update(self, isim):
    self.cur.execute("UPDATE hakemler SET durum='UYGUNSUZ' WHERE isim='%s' ",isim)
    self.conn.commit()

关于Python 不更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58192564/

相关文章:

python - 这个 while 循环是如何退出的?

python - Pandas 填充: How to fill only leading NaN from beginning of series until first value appears?

Python Float 舍入错误

MySQL Workbench 用户输入

mysql - Left Join with Where 子句返回空结果

python - 省略 Toplevel 的父参数

python - 使用 updateCells API 请求更新多个单元格的格式

mysql - 如何使用nodejs和mysql插入多个对象?

python tkinter 打包

python - 如何让 TK 按钮命令接受带有变量的参数(Python)