简体   繁体   English

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

[英]Delete Rows selected in a python GUI treeview and then have it deleted from sqlite3 database as well

I am using tkinter. 我正在使用tkinter。 I have a treeview and I am displaying data in the treeview from a database from SQlite DB. 我有一个树形视图,并且正在从SQlite DB的数据库中以树形视图显示数据。 However, I added a delete function to be able to select values from the treeview and then delete them with the delete function and button. 但是,我添加了删除功能,以便能够从树视图中选择值,然后使用删除功能和按钮将其删除。 It deletes from the treeview but it doesn't delete from the database. 它从树视图中删除,但不从数据库中删除。

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")

I get the same error: 我犯了同样的错误:

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.

I am curious on why you are using selected_item = tree1.selection_set() . 我很好奇您为什么使用selected_item = tree1.selection_set()

From the docs: 从文档:

selection_set(*items) selection_set(*项)

items becomes the new selection. 项目成为新选择。

Changed in version 3.6: items can be passed as separate arguments, not just as a single tuple. 在版本3.6中更改:项目可以作为单独的参数传递,而不仅仅是单个元组。

It looks to me you should be using tree.selection() instead. 在我看来,您应该使用tree.selection()代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM