2017-06-20 142 views
0

這怎麼可能?查詢無法正常工作。我質疑的「僱用日期」,這是日期/時間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

在微軟的SQL味道,你需要使用的奇怪名稱的列括號(含空格,連字符,保留字等)。

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

感謝的人,作品像一個魅力。 –