1

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.

0

2 Answers 2

3

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))
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe this works

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.