sqlite Tkinter:更新文本框中的值时出现问题,任何错误,但数据库不更新

tf7tbtn2  于 12个月前  发布在  SQLite
关注(0)|答案(1)|浏览(119)

我有一些文本框(textbox1textbox2),我需要不断更新它们的值。我已经创建了一个数据库,其中包含值。我希望当我在两个文本框中写入一个值时,数据库中相应的值随后会更新。考虑到我将插入许多文本框,我想每个文本框自动对应到它在数据库中的行
我没有得到任何错误,但数据库不更新
下面是数据库:

CREATE TABLE "valuedb" (
"id" INTEGER,
"all_value" INTEGER,
PRIMARY KEY("id")
);

Python代码是:

import sqlite3
from tkinter import ttk
import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.geometry("130x100")

#Textbox
textbox1 = ttk.Entry(root, width=7)
textbox1.place(x=1, y=1)

textbox2 = ttk.Entry(root, width=7)
textbox2.place(x=1, y=30)

#Save
def save():
    con = sqlite3.connect("/home/MyPc/Destktop/my_db2")
    cursor = con.cursor()

    cursor.execute("UPDATE valuedb SET all_value=?", (textbox1.get(), textbox2.get(),))

    conn.commit()
    conn.close()

    messagebox.showinfo('Success', 'Saved and Update')

#SAVE BUTTON
save = tk.Button(root, text="Save & Update", bg='#b40909', foreground='white', command= save)
save.place(x=1, y=70)

#load()

root.mainloop()
tuwxkamq

tuwxkamq1#

您正在创建查询,但没有传入查询所需的参数

#Save
def save():
    conn = sqlite3.connect("/home/Qi_Yao/Desktop/my_db.db")
    c = conn.cursor()

    if c:
        c.execute("UPDATE valuedb SET all_value=? WHERE id=?;", (textbox1.get(), textbox2.get()))
    else:
        c.execute("INSERT INTO valuedb VALUES (?);", (textbox1.get(),)) # Should be (?, ?) because the table has two fields as seen in the update query?
  
    conn.commit()
    conn.close()

    messagebox.showinfo('Success', 'Saved and Update')

这应该可以解决项目没有保存到数据库的问题,但是您可能应该检查您的逻辑,看看您希望if c:发生什么。

相关问题