简体   繁体   中英

Python <> MS ACCESS filter by date

I connected python to MS Access using pyodbc

I want to delete rows from table based on date criteria (date greater then)

my code:

conn = pyodbc.connect(
r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=C:\***\Test.accdb')
cursor = conn.cursor()
cursor.execute('DELETE FROM `# Euromedica` WHERE Date > 2022-01-09')
cursor.commit()

after code execution I have an empty table. How can I properly filter by Date?

Date() is a function which returns the current system date. If you must keep Date as a field name in your table, notify the db engine you mean the field name instead of the function in your SQL statement. You can do that by enclosing the field name in square brackets or backticks, or by qualifying the name with the table name or alias.

Access SQL will interpret 2022-01-09 to mean 2022 minus 1 minus 9. If you want it interpreted as a Date/Time value, add # delimiters as the commenters suggested.

cursor.execute('DELETE FROM `# Euromedica` AS e WHERE e.Date > #2022-01-09#')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM