How do I delete or edit records from data base
You are using literal sql script and basically you can delete
and update
like below.
An advice ORM (Object Relational Mapping)
is more effortless rather than raw query.
For Delete
DELETE <TableName> WHERE Id = <Value>
conn = sqlite3.connect("Imenik.db")c = conn.cursor()sql = "DELETE TableName WHERE Id = ?"id = 1c.execute(sql, (id,))
For Update
UPDATE <TableName> SET <ColumnName> = <NewValue> WHERE Id = <Value>
conn = sqlite3.connect("Imenik.db")c = conn.cursor()sql = "UPDATE TableName SET ColumnName = ? WHERE Id = ?"id = 1new_value = 'hello'# params order left to right (important).c.execute(sql, (new_value, id))
Hi and welcome to Stack Overflow. For you to know which data is selected with the mouse, you can use the following way:
name_of_listbox.curselection()
This will return a list of the contents of the rows currently selected. It gives us a list because there is an option to select multiple rows of data in a ListBox
.You can iterate through each and grab the data out of them.Store the data in a variable and use them for UPDATE
and DELETE
commands.
EDIT:
For deleting records:
f_name = listbox.curselection()[0]l_name = listbox.curselection()[1]number = listbox.curselection()[2]c1.execute("DELETE * FROM Kontakti WHERE f_name = ? AND l_name = ? AND number = ?", (f_name, l_name, number))conn1.commit()
For modifying the values:
c1.execute("UPDATE Kontakti SET f_name=?, l_name=?, number=? WHERE f_name= ? AND l_name = ? AND number=?",(new_value_for_f_name,new_value_for_l_name,new_value_for_number, f_name, l_name, number))conn1.commit()
I quite did not get the way you were selecting first name, last name and number from a single listbox. So I have kept the name of the listbox the same.