How to Update to a DB Sqlite3 with POO?

Asked

Viewed 235 times

0

I have a product registration program with everything working only I lack the update, I can update edit the data in the table and in the bank but it is not working right.

It edits all the data and not just the data I want. If I type the name and leave the rest blank it changes the name and deletes the other fields.

So my question is what should I change in this function for it to work by editing only the selected field?

    def editarProd(self):
        self.banco = sqlite3.connect('Vendas.db')
        self.cursor = banco.cursor()
        query = "SELECT * FROM Produtos"
        result = self.banco.execute(query)
        for row_number in enumerate(result):
            if row_number[0] == self.listaprodutos.currentRow():
                data = row_number[1]
                Nome = data[0]
                Nome2 = self.produtotext.text()
                Quant2 = self.estoquetext.text()
                Valor2 = self.precocustotext.text()
                Preco2 = self.precovendatext.text()
                Forn2 = self.fornecedorcomboBox.currentText()
                self.banco.execute(f"UPDATE Produtos SET Nome='{Nome2}', Quant='{Quant2}', Valor='{Valor2}', Preco='{Preco2}', Forn='{Forn2}' WHERE Nome='{Nome}'")
                self.banco.commit()
                self.LoadDatabase()

if you want to see the full code is all commented

https://pastebin.com/ZdHgqkUG

  • Did you not see that it was already solved? Look at the answers has nothing outstanding or too wide.

2 answers

0


Your problem should be in the sql command

print() the command and see what is being sent

Your sql comand is updating all fields

UPDATE 
Produtos 

SET 
Nome='{Nome2}', 
Quant='{Quant2}', 
Valor='{Valor2}', 
Preco='{Preco2}', 
Forn='{Forn2}' 

WHERE 
Nome='{Nome}'

You have two alternatives

  1. Keeping the code, but you must bring all the fields and then update it

  2. The other option is to update only the field you will use, in this case if the field is empty, you will not work with it;

enter the code here

UPDATE 
Produtos 

SET 
Nome='{Nome2}' 

WHERE 
Nome='{Nome}'

Make a copy of your 'Sales.db' bank Perform sql tests before entering the code On the website below, you can test your commands online http://kripken.github.io/sql.js/examples/GUI/

0

I solved the problem by adding the most code to the function.

follows corrected function

    def editarProd(self):
        self.banco = sqlite3.connect('Vendas.db')
        self.cursor = banco.cursor()
        query = "SELECT * FROM Produtos"
        result = self.banco.execute(query)
        for row_number in enumerate(result):
            if row_number[0] == self.listaprodutos.currentRow():
                data = row_number[1]
                Idprod = data[0]
                Nome2 = self.produtotext.text()
                Quant2 = self.estoquetext.text()
                Preco2 = self.precocustotext.text()
                Valor2 = self.precovendatext.text()
                Forn2 = self.fornecedorcomboBox.currentText()
                if Nome2 !='':
                    self.banco.execute(f"UPDATE Produtos SET Nome='{Nome2}' WHERE Id={Idprod}")
                if Quant2 != '':
                    self.banco.execute(f"UPDATE Produtos SET Quant='{Quant2}' WHERE Id={Idprod}")
                if Valor2 != '':
                    self.banco.execute ( f"UPDATE Produtos SET Valor='{Valor2}' WHERE Id={Idprod}" )
                if Preco2 != '':
                    self.banco.execute( f"UPDATE Produtos SET Preco='{Preco2}' WHERE Id={Idprod}" )
                if Forn2 != '':
                    self.banco.execute( f"UPDATE Produtos SET Forn='{Forn2}' WHERE Id={Idprod}" )
                self.banco.commit()
                self.LoadDatabase()
                self.limparcampos ( )

I based my response on that of Ruben Borges who showed me the way forward.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.