Subtract values using sqlite in Python

Asked

Viewed 96 times

1

I want to do the following:

Create a function with a quantity parameter, thus causing the value of the parameter to subtract with a value from a table field:

Example: sell(5), this 5 will subtract with table quantity field.

The closest I got was:

def vender(self, nome, quantidade):
    consulta = 'UPDATE OR IGNORE Produtos SET quantidade = quantidade-1'
    self.cursor.execute(consulta, ())
    self.conexao.commit()

Only in this case it decreases 1 in 1.

  • Hi, Voce is subtracting quantity-1, you have to first check the quantity and subtract the amount from the result.

  • Hello, the mode that G.Bittencourt posted below solved the problem, only now it is giving conflict with the registration function, when I will register a product of the following error....self.cursor.execute(query, (name,category,value,quantity)) sqlite3.Operationalerror: near "?": syntax error

  • I was able to solve it, I only had to open and close parentheses within VALUES ? ,? ,? ,? resulting in VALUES(?,?,??)... Thank you very much

1 answer

1


You can pass the variable quantidade in your query variable:

consulta = "UPDATE OR IGNORE Produtos SET quantidade = quantidade - %d" %quantidade

  • Hi, thanks a lot, but there was an error in another part of the program. When registering a product the following error appears.... Traceback (Most recent call last): File "/home/Ayrton/Documents/Store/main.py", line 5, in <module> bank.cadastrar('Iphone', 'Celular', 2.100, 100) File "/home/Ayrton/Documents/Store/products.py", line 12, in cadastrar self.cursor.(query, (name, category, value, quantity)) sqlite3.Operationalerror: near "%": syntax error.... This occurred right after this issue you suggested and solved the other issue

  • I was able to solve it, I only had to open and close parentheses within VALUES ? ,? ,? ,? resulting in VALUES(?,?,??)... Thank you very much

Browser other questions tagged

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