Python Forum
PyQt Selected row in Table Widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt Selected row in Table Widget
#1
   
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)
Reply
#2
(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?
Reply
#3
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)
Reply
#4
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] coloring the widget table row atlass218 5 13,662 Aug-20-2019, 07:57 PM
Last Post: Axel_Erfurt
  PyQt, Open a Table when a row is selected populating it with the row values rarevesselt 18 15,163 Mar-30-2019, 12:57 AM
Last Post: rarevesselt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020