简体   繁体   中英

Update Sqlite3 in Python 3

I have an object oriented function to update an inventory table, but when having execute the function by clicking the button it does not update and crashes the program, can anyone see me an error in this function? I am selecting the product to update in a QtableWidget

    def editarProd(self):
    cursor = banco.conexao.cursor()
    query = """SELECT E.IDPRODUTO,
                      E.CODBARRA, E.PRODUTO, 
                      C.CATEGORIA, printf("%.2f",E.ESTOQUE), 
                      printf("%.2f",E.ESTOQUE_MIN), printf("%.2f",E.VALOR_CUSTO), 
                      printf("%.2f",E.VALOR_VENDA), printf("%.2f",E.VALOR_VENDA-E.VALOR_CUSTO) AS "LUCRO", 
                      F.FORNECEDOR 
                      FROM ESTOQUE E 
                      INNER JOIN FORNECEDOR F
                      ON E.ID_FORNECEDOR = F.IDFORNECEDOR
                      INNER JOIN CATEGORIA C
                      ON E.ID_CATEGORIA = C.IDCATEGORIA
                      ORDER BY E.PRODUTO"""
    result = cursor.execute(query)
    for row_number in enumerate(result):
        if row_number[0] == self.listaprodutos.currentRow():
            data = row_number[1]
            IdProd = data[0]
            codbarra = self.codigotext.text()
            produto = self.produtotext.text()
            estoque = self.estoquetext.text()
            estoquemin = self.estoquemintext.text()
            valorcusto = self.precocustotext.text()
            valorvenda = self.precovendatext.text()
            Forn = self.fornecedorcomboBox.currentData()
            Cat = self.categoriacomboBox.currentData()
            try:
                cursor.execute = (f"""UPDATE ESTOQUE
                SET CODBARRA ='{codbarra}',
                    PRODUTO ='{produto}',
                    ESTOQUE = {estoque},
                    ESTOQUE_MIN = {estoquemin}, 
                    VALOR_CUSTO = {valorcusto},
                    VALOR_VENDA = {valorvenda},
                    ID_CATEGORIA = {Cat},
                    ID_FORNECEDOR = {Forn} 
                    WHERE IDPRODUTO = {IdProd}""")
                banco.conexao.commit()
                self.LoadDatabase ( )
                self.limparcampos ( )
            except Exception:
                msg = QMessageBox ( )
                msg.setText ( "Preencha os Campos" )
                msg.setWindowTitle ( "Dados não inseridos!" )
                msg.setStandardButtons ( QMessageBox.Ok | QMessageBox.Cancel )
                msg.exec_ ( )

I solved my problem with some changes and it is working perfectly. Follows the code.

    def editarProd(self):
    try:
        cursor = banco.conexao.cursor()
        query = ("""SELECT IDPRODUTO, CODBARRA, PRODUTO, 
                          ID_CATEGORIA, ESTOQUE, 
                          ESTOQUE_MIN, VALOR_CUSTO, 
                          VALOR_VENDA, ID_FORNECEDOR
                          FROM ESTOQUE
                          ORDER BY PRODUTO""")
        result = cursor.execute(query)
        for row_number in enumerate(result):
            if row_number[0] == self.listaprodutos.currentRow():
                data = row_number[1]
                IdProd = data[0]
                codbarra = self.codigotext.text()
                produto = self.produtotext.text()
                estoque = self.estoquetext.text()
                estoquemin = self.estoquemintext.text()
                valorcusto = self.precocustotext.text()
                valorvenda = self.precovendatext.text()
                Forn = self.fornecedorcomboBox.currentData()
                Cat = self.categoriacomboBox.currentData()
                cursor.execute ( f"""UPDATE ESTOQUE SET CODBARRA='{codbarra}',
                                                        PRODUTO='{produto}',
                                                        ESTOQUE={estoque},
                                                        ESTOQUE_MIN={estoquemin},
                                                        VALOR_CUSTO={valorcusto},
                                                        VALOR_VENDA={valorvenda},
                                                        ID_CATEGORIA={Cat},
                                                        ID_FORNECEDOR={Forn}
                                                        WHERE IDPRODUTO={IdProd}""")
                banco.conexao.commit()
                self.LoadDatabase()
    except Exception:
        print('Erro!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM