Behdad Nayebi
Behdad Nayebi

Reputation: 23

Execute an insert query include date into access by python

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

Upvotes: 2

Views: 509

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123839

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)")

Upvotes: 1

Related Questions