0

I'm new to Python (used to C#) and need to use an Access database (.accdb) with it.

The syntax for building SQL Queries is also a bit odd for me.

I have the following:

def updateSQL(table,keyField,keyVal,field,newVal):
 sqlCommand = "UPDATE " + table + " SET (?)=(?) WHERE (?)=(?);"
 crsr.execute(sqlCommand, (field, newVal, keyField, keyVal))
 crsr.commit()
 print("Tables update successfully")

But for some reason I'm getting the following error:

[Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement

I've tried a few different things with the statement and I can't for the life of me work out where it's going wrong, any ideas?

Gord Thompson
125k39 gold badges252 silver badges458 bronze badges
asked Mar 10, 2017 at 20:47

1 Answer 1

3

The '?' markers are for values, but the column name is not a value. You never want to put values into your SQL, but you will need put the columns. Try something like:

sql = 'update {} set {}=? where {}=?'.format(table, field, keyField)
cursor.execute(sql, newVal, keyVal)
answered Mar 10, 2017 at 23:30
Sign up to request clarification or add additional context in comments.

1 Comment

For protection against "unfortunate" table/column names, [{}] would be preferable to plain old {}.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.