简体   繁体   中英

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

I am using tkinter. I have a treeview and I am displaying data in the treeview from a database from 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() .

From the docs:

selection_set(*items)

items becomes the new selection.

Changed in version 3.6: items can be passed as separate arguments, not just as a single tuple.

It looks to me you should be using tree.selection() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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