PyQt表小部件从表和数据库中删除选定的行

时间:2018-12-05 08:52:38

标签: python sqlalchemy pyqt

enter image description here

enter image description here

在这行代码上我需要帮助。我想选择一行并将其从表中以及从数据库中删除。

def deleteProduct(self):

    row = self.products_table.currentRow()

    if row > -1:
        currentproductid = (self.products_table.item(row, 0).text(), )
        query = session.query(Product).filter(Product.product_id==currentproductid).first()
        session.delete(query)
        session.commit()

        self.mainTable.removeRow(currentRow)

我收到此错误:

    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>

1 个答案:

答案 0 :(得分:0)

    def deleteProduct(self):
        row = self.products_table.currentRow()

        currentproductid = (self.products_table.item(row, 0).text() )
        product_name = (self.products_table.item(row, 1).text() )
        query = session.query(Product).filter(Product.product_id==str(currentproductid)).first()
        session.delete(query)
        session.commit()
        self.products_table.removeRow(row)