Home > Back-end >  SQL update keeps spitting syntax errors when my code seems properly formatted
SQL update keeps spitting syntax errors when my code seems properly formatted

Time:04-07

I have this piece of code that tries to update the date column with ID = m. m is an integer as it's supposed to be and for date I have tried both a string and a datetime.date.

  sql = '''UPDATE DaByDay
           SET Date = ?
           WHERE ID = ?'''
  crsr.execute(sql, date, m)
  cnxn.commit()

Yet I keep getting

Traceback (most recent call last):
File "C:\Users\USERNAME\Desktop\database test.py", line 47, in
crsr.execute(sql, date, m)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement. (-3503) (SQLExecDirectW)')

Now how should I go about solving this?

CodePudding user response:

Should avoid using reserved words as names. Date is a reserved word in Access. Enclose field name in brackets [Date].

In addition, if name uses space or special characters also enclose in brackets.

Probably unrelated to issue but could use a quote mark instead of triple apostrophe.

CodePudding user response:

Considering that the proper syntax for date literals is 'YYYY-MM-DD' (within quotes) or #mm/dd/yyyy# (within hash marks) you should try supplying in that manner. Also Date is a reserved word in Access you should use square brackets around it.

  • Related