Python不会更新表

时间:2019-10-01 21:35:29

标签: 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)

没有错误判断或类似的东西。代码就是行不通的

1 个答案:

答案 0 :(得分:0)

您的问题在这里:

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

数据库操作要求您在操作后commit,以便将更改写入数据库。您已经颠倒了顺序。替换订单:

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