在Python中遇到SQLite问题

时间:2016-08-14 11:13:50

标签: python sqlite pyqt

我想在SQLite表中添加一个值列表。为了更准确,我想在Stock列中添加它。但是在运行我的应用程序时遇到了一些问题。这是应用程序运行之前的数据库。

enter image description here

当我尝试运行我的程序时,我得到了错误的列数据。这是我的代码

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()

1 个答案:

答案 0 :(得分:1)

如果要更改行的值,只需让数据库在内部为您执行此操作:

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()