Oleksandr Ivanenko Oleksandr Ivanenko - 3 months ago 7
Python Question

Have problems with SQLite in Python

I want to add into my SQLite table a list of values. To be more accurate, I want to add it in Stock column. But I have some problems when running my app. Here is my database before app running. And when I try running my programm, I got wrong column data. Here is my code

def count(ui):
i = []
z = 0
v = 0
if ui.comboBox.currentText() == '50НР4':
cur.execute("""SELECT Part, Stock FROM Details WHERE Pumps = '50НР4' OR Pumps = '50НР4/6'""")
elif ui.comboBox.currentText() == '50НР6.3':
cur.execute("""SELECT Part, Stock FROM Details WHERE Pumps = '50НР6' OR Pumps = '50НР4/6'""")
for row, form in enumerate(cur):
i.append(form[1])
for element in i:
i[z] -= 1
z += 1

if ui.comboBox.currentText() == '50НР4':
z = 0
for elem in i:
cur.execute("""UPDATE Details SET Stock = (?) WHERE Pumps = '50НР4' OR Pumps = '50НР4/6' AND ROWID = (?)""",
[i[z], v])
v += 1
z += 1
elif ui.comboBox.currentText() == '50НР6.3':
z = 0
for elem in i:
cur.execute("""UPDATE Details SET Stock = (?) WHERE Pumps = '50НР6' OR Pumps = '50НР4/6' AND ROWID = (?)""",
[i[z], v])
v += 1
z += 1
elif ui.comboBox.currentText() == '50НР4' or ui.comboBox.currentText() == '50НР6':
z = 0
for elem in i:
cur.execute("""UPDATE Details SET Stock = (?) WHERE Pumps = '50НР4/6' AND ROWID = (?)""", [i[z], v])
v += 1
z += 1
print(i)
con.commit()

Answer

If you want to change the values of rows, just let the database do it for you internally:

def count(ui):
    if ui.comboBox.currentText() == '50НР4':
        condition = "Pumps = '50НР4' OR Pumps = '50НР4/6'"
    elif ui.comboBox.currentText() == '50НР6.3':
        condition = "Pumps = '50НР6' OR Pumps = '50НР4/6'"
    else:
        condition = "Pumps = '50НР4/6'"
    cur.execute("""UPDATE Details SET Stock = Stock - 1 WHERE %s""" % condition)
    con.commit()