Reputation: 45
So i need to update data in a database but im getting an error i dont understand. What i have is this
def update_account_balance(db, number, account, change):
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute('UPDATE Accounts SET (?) = (?) WHERE Number == (?)', [account, change, number])
and what im getting is:
Traceback (most recent call last):
Python Shell, prompt 2, line 1
File "/Users/aharonsnyder1/Desktop/assignment 2/banking.py", line 168, in <module>
cur.execute('UPDATE Accounts SET (?) = (?) WHERE Number == (?)', [account, change, number])
sqlite3.OperationalError: near "(": syntax error
I am not sure what it is not liking.
Upvotes: 1
Views: 70
Reputation: 107567
Several things:
WHERE
clauses are passed in the second argument not the structural component of the SQL statement like column name.Consider revising where you format update statement string with column name (assuming column name is the value in account
variable) and then pass parameters:
cur.execute('UPDATE Accounts \
SET [{0}] = ? \
WHERE Number = ?'.format(account), (change, number))
Upvotes: 3
Reputation: 2124
Maybe this works
cur.execute('UPDATE Accounts SET (?) = (?) WHERE Number == (?)', (account, change, number))
Upvotes: 1