MS Access PYODBC查询

时间:2017-06-20 19:16:42

标签: ms-access pyodbc

这怎么可能?查询无法正常工作。我查询了“雇用日期”,即日期/时间

import pyodbc
import datetime
conn_str = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Administrator\Downloads\Access2010DB\GarageMike.accdb;'
cnxn = pyodbc.connect(conn_str)
crsr = cnxn.cursor()
# date_str = '06-06-2000'
# date = datetime.datetime.strptime(date_str,'%d-%m-%Y')
# print date
sql = """
SELECT * FROM Employee WHERE 'Hire Date' >= ? 
"""
params = (datetime.date(2007, 8, 8))
crsr.execute(sql, params)
for row in crsr:
    print row
# crsr.execute("select * from Employee where 'Hire Date' <= '{date}'".format(date=date))
# for row in crsr:
#   print row

This is my code which yields the output :

(1, u'LeBeau', u'Sam', u'Technician', u'333-444-5555', u'444-444-5555', u'99 Ninth St', u'Somewhere', u'ST', u'55555', u'USA', datetime.datetime(2000, 6, 6, 0, 0))
(2, u'James', u'Carl', u'Helper', u'555-555-4444', None, u'33 Third Ave', u'Somewhere', u'ST', u'55555', u'USA', datetime.datetime(2007, 7, 7, 0, 0))
[Finished in 0.2s]

1 个答案:

答案 0 :(得分:1)

在Microsoft的SQL风格中,您需要为奇数命名的列使用括号(包含空格,连字符,保留字等)。

sql = """
SELECT * FROM Employee WHERE [Hire Date] >= ? 
"""