2

I am trying to execute a query to insert two values into a simple table in an access database. But I got this error:

pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. (-3502)(SQLExecDirectW)')

How should I write the date format? My code is like below (The code is working with only integers to insert so I think my date format is no correct):

import pyodbc

conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=C:\Users\behdad\Documents\Database1.accdb;')
cursor = conn.cursor()
cursor.execute("insert into Table1 (date, number) VALUES( '2021-02-05 00:00:00', 24)")

The sample table is here: date format in access

3
  • Try using # instead ' for delimiters. Date is a reserved word. Advise not to use reserved words as field names. If still have an issue, try [date]. Commented Feb 4, 2021 at 11:13
  • I do these tricks but it still has errors Commented Feb 4, 2021 at 11:35
  • Thanks. It's working Commented Feb 4, 2021 at 13:39

1 Answer 1

1

date and number are both reserved words in Access SQL, so if you have columns with those names you will need to enclose the names in square brackets:

cursor.execute("insert into Table1 ([date], [number]) VALUES( '2021-02-05 00:00:00', 24)")
Sign up to request clarification or add additional context in comments.

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.