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

时间:2019-02-11 16:30:09

标签: python database sqlite tkinter treeview

我正在使用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.

1 个答案:

答案 0 :(得分:0)

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

从文档中

  

selection_set(*项)

     

项目成为新选择。

     

在3.6版中进行了更改:项目可以作为单独的参数传递,而不是   就像一个元组一样。

在我看来,您应该改用tree.selection()