Python Forum

Full Version: PyQt Selected row in Table Widget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[attachment=502]
How can I dynamically select row and get the product id value for the selected product to be passed into the query?



def deleteProduct(self):
        row = self.products_table.currentRow()
        if row > -1:
           product_id = (self.products_table.item(row, 0).text(), )
           query = session.query(Product).filter(Product.product_id=='product_id').first()
           session.delete(query)
           session.commit()
            #self.dbCursor.execute("""DELETE FROM Main WHERE username=?""", currentUsername)
            #self.dbConn.commit()
           self.products_table.removeRow(row)
(Dec-04-2018, 09:45 AM)rarevesselt Wrote: [ -> ][attachment=502] How can I dynamically select row and get the product id value for the selected product to be passed into the query?
def deleteProduct(self): row = self.products_table.currentRow() if row > -1: product_id = (self.products_table.item(row, 0).text(), ) query = session.query(Product).filter(Product.product_id==product_id).first() session.delete(query) session.commit() #self.dbCursor.execute("""DELETE FROM Main WHERE username=?""", currentUsername) #self.dbConn.commit() self.products_table.removeRow(row)

Am getting this error :
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.InterfaceError: <exception str() failed>
How can I resolve this error?
what is

if row > -1:

and the comma in

(self.products_table.item(row, 0).text(), )

i would check if there is a selection and remove the comma

def deleteProduct(self):
        if self.products_table.selectionModel().hasSelection():
           row = self.products_table.currentRow()
           product_id = (self.products_table.item(row, 0).text())
           query = session.query(Product).filter(Product.product_id=='product_id').first()
           session.delete(query)
           session.commit()
            #self.dbCursor.execute("""DELETE FROM Main WHERE username=?""", currentUsername)
            #self.dbConn.commit()
           self.products_table.removeRow(row)
Thanks for your response.This is the working code.Thanks Axel_Erfurt.I removed the comma too.
Quote:The row>-1 too does not do anything.
def deleteProduct(self):
        row = self.products_table.currentRow()
        currentproductid = (self.products_table.item(row, 0).text() )
        query = session.query(Product).filter(Product.product_id==str(currentproductid)).first()
        session.delete(query)
        session.commit()
        self.products_table.removeRow(row)