简体   繁体   中英

How to add number to sqlite3 column values

Am able to to successfully update sqlite3 column values with this function

fgr = 1043

con = sqlite3.connect("test.db")
cur = con.cursor()
cur.executescript("UPDATE '{}' SET BALANCE=BALANCE+50".format(fgr))
con.commit()
con.close()

What i want to update it using the row Id greater than 4 , when i run the function it execute successfully but when the check the data in the column no changes has been applied.

Funtion

fgr = 1043

con = sqlite3.connect("test.db")
cur = con.cursor()
cur.executescript("UPDATE '{}' SET BALANCE=BALANCE+50".format(fgr))
cur.executescript("UPDATE '{}' SET BALANCE=BALANCE+50 WHERE ID > ?".format(fgr, (4),))
con.commit()
con.close()

I suspect that you are building your update query incorrectly. Try this version:

fgr = 1043
ID = 4
cur.executescript("UPDATE \"" + fgr + "\" SET BALANCE = BALANCE + 50 WHERE ID > ?", (ID,))

If the above still update no records, then maybe you don't have any matching records in your SQLite database. Try to confirm this by running the following select:

SELECT COUNT(*)
FROM 1043
WHERE ID > 4;

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