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.
    Need help!!!!

    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

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you are using the SQLite API you should ask the authors of that library directly.

    Otherwise, you could consider using Qt's SQL module with a QListView and a QSqlTableModel.

    On a side note, what does data3 contain ? id usually 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


  • Lifetime Qt Champion

    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, 192.198.184.123, 80, 192.198.184.176, 443, bm, bad).


  • Lifetime Qt Champion

    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 data3 doesn't contain 11 but something similar to (1, input, eth0, wlan1, tcp, 192.198.184.123, 80, 192.198.184.176, 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.


  • Moderators

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.