How to delete selected row in QListWidget(pyqt/python) from table(sqlite3 database)??
i am not getting error but the data is not deleting in the database.
conn = sqlite3.connect('db_iptable') c = conn.cursor() data3 = self.w.listWidget.SelectedCurrentRow().text() data3 = str(data3) query = "DELETE FROM rules WHERE id = '%s';" % data3.strip() mydata = c.execute(query) conn.commit() conn.close
Hi and welcome to devnet,
Since you are using the SQLite API you should ask the authors of that library directly.
On a side note, what does data3 contain ?
idusually implies that it's the primary key thus a number.
Here is my database table : ("CREATE TABLE IF NOT EXISTS rules (id INTEGER PRIMARY KEY AUTOINCREMENT, chain TEXT, iface_in TEXT, iface_out TEXT, protocol TEXT, dst_addr TEXT, dst_port INTEGER, src_addr TEXT, src_port INTEGER, algo TEXT, string_match TEXT)")
data3 will contain all the values in the selected row which is 11. i am wrong?? i am new in programming field.thanks
I don't know. You're the author of that application so you know the content of your database and your application internals. So is that 11 a valid value ?
example of a row : (1, input, eth0, wlan1, tcp, 184.108.40.206, 80, 220.127.116.11, 443, bm, bad).
Do you realise that up until now, you are just giving minor elements about your application and the only Qt related part is the
data3 = self.w.listWidget.SelectedCurrentRow().text()which is working ?
If that's a row in your listWidget, then that likely means that
11but something similar to
(1, input, eth0, wlan1, tcp, 18.104.22.168, 80, 22.214.171.124, 443, bm, bad)hence your query failing.
my means was 11 elements in the selected row. not 11 as a value. i want to click on the row in the qlistwidget and delete it.
@tchaler Sorry, but what do you mean by eleven elements in the selected row? You want to delete a row using it's primary key, right?
In this case your query is wrong:
query = "DELETE FROM rules WHERE id = '%s';" % data3.strip()
You are passing the id a string, I think it should be
query = "DELETE FROM rules WHERE id = %s;" % data3.strip()
Do you store the primary key in the list view? I mean you somehow need to get the id from selected row.