I am making a program for sales, which is connected to a SQLite3 database. I'm doing something similar to what someone did on youtube. But my difference is that I want my program to show the id and the amount. Here I leave all the code and the errors that I get.
def delete_product(self):
self.mesagge['text'] = ''
try:
self.tree.item(self.tree.selection())['values'][0]
except IndexError as e:
self.mesagge['text'] : 'Selecciona un importe'
return
self.mesagge['text'] = ''
name = self.tree.item(self.tree.selection())['values']
query = 'DELETE FROM importes WHERE id = ?'
self.run_query(query, (name, ))
self.mesagge['text'] = 'El importe fue eliminado'
self.get_products()
I hope you can help me!
The original error causes
sqlite3.Connection.cursor.execute
it and it is due to the fact that a nested list or tuple is passed as a parameter to the query, which can only be usedsqlite3.Connection.cursor.executemany
in such a way that each nesting would be a query.When a row is obtained from a
ttk.TreeView
methodttk.TreeView.item
, a dictionary of the form is obtained:what is relevant to the cell contents are:
The key
"text"
: stores the text of the first column (always identified with#0
).The key
"values"
: stores the value of the rest of the columns and is always a list if there is at least one row selected, otherwise it is an empty string.When you do:
name
it's a list and that's why itexecute
cries bitterly... To get the value of a specific column, other than the first one, you must index:this would get the value of the second column of the TreeView.
Therefore, if your column with the ids is the first one, you must do: