Aharon Snyder
Aharon Snyder

Reputation: 45

Updating data in a database in python

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

Answers (2)

Parfait
Parfait

Reputation: 107567

Several things:

  1. SQL syntax does not use the double equal sign
  2. SQLite uses either quotes, backticks, or square brackets to denote keywords.
  3. Only parameters usually in 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

GAVD
GAVD

Reputation: 2124

Maybe this works

cur.execute('UPDATE Accounts SET (?) = (?) WHERE Number == (?)', (account, change, number))

Upvotes: 1

Related Questions