删除在python GUI树视图中选择的行,然后从sqlite3数据库中删除它

问题描述 投票:0回答:1

我正在使用tkinter。我有一个树视图,我正在从SQlite DB的数据库中显示树视图中的数据。但是,我添加了一个删除功能,可以从树视图中选择值,然后使用删除功能和按钮删除它们。它从树视图中删除,但不会从数据库中删除。

def delete():
name2 = name_1.get()
phone2 = phone_number.get()
conn2 = sq.connect('Clients.db')
c2 = conn2.cursor()
selected_item = tree1.selection_set()
query = "DELETE FROM clients WHERE name=? AND phone=?"
c2.execute(query,(selected_item,))
conn2.commit()
tree1.delete(selected_item)






root = Tk()
root.geometry("800x800")
root.title("Hello")
root.configure(background="powder blue")
header = Label(root, text = "Clients Database",    font=("arial",30,"bold")).pack()

con = sq.connect('Clients.db')
c = con.cursor()
c.execute("CREATE TABLE IF NOT EXISTS clients (name TEXT, phone TEXT)")
con.commit


tree1 = ttk.Treeview(root, height=10, columns=("Name", "PhoneNumber"),   show=["headings"])
tree1.column('Name', anchor=W)
tree1.column('PhoneNumber', anchor=W)
tree1.heading('Name', text="Name")
tree1.heading('PhoneNumber', text="Phone Number")

我犯了同样的错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/zizibaby/Desktop/Ingredients .py", line 38, in delete
c2.execute(query,(selected_item,))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The  current statement uses 2, and there are 1 supplied.
python database sqlite tkinter treeview
1个回答
0
投票

我很好奇你为什么使用selected_item = tree1.selection_set()

来自文档:

selection_set(*项)

物品成为新的选择。

在版本3.6中更改:项目可以作为单独的参数传递,而不仅仅作为单个元组传递。

在我看来你应该使用tree.selection()代替。

© www.soinside.com 2019 - 2024. All rights reserved.