我正在使用pypyodbc从访问数据库中选择数据.我使用以下查询与三个已指定的参数.
我试过几个品种,但无济于事.我没有看到我的语法有什么问题.
SELECT [Date], [Time], [uSec], [threeR], [twoCV]
FROM [table_a]
WHERE (Date = ? AND Time > ?)
OR (Date > ?)
参数具有以下类型:
[datetime.date, datetime.time, datetime. date]
哪,打印时:
1900-09-16 , 00:00:00, 1900-09-16
pypyodbc.DatabaseError:(‘07002′,'[07002] [Microsoft] [ODBC Microsoft Access驱动程序]参数太少.预计4.’)
#-- Begin Python code sample
#-- Checks the DB file and retrieves data
def pullData(self):
#-- Connect to Access
con = pypyodbc.connect('Driver={Microsoft Access Driver (*.mdb)};DBQ=F:/database.mdb')
cur = con.cursor()
#-- Get column list
columnListODBC = '[thisDate], [thisTime]'
for y in myTable.getColumns():
columnListODBC = columnListODBC + ', [' + y + "]"
#-- See footnote 1
print(columnListODBC)
#-- Get the most recent SQL entry
for row in curSQL.execute('SELECT MAX(Datetime) FROM [' + _.getName() + ']'):
xDateTime = datetime.datetime.strptime(row[0], "%Y-%d-%m %H:%M:%S")
day = xDateTime.date() # Get only the DATE of the most recent entry
time = xDateTime.time() # Get only the TIME of the most recent entry
#-- Pull all ODBC data
queryString = 'SELECT ' + columnListODBC + ' FROM [' + _.getName() + '] WHERE (thisDate = ? AND thisTime > ?) OR (thisDate > ?)'
#-- See footnote 2
print(queryString, ", ", day, ", ", time)
cur.execute(queryString, [day,time,day])
打印1:[thisDate],[thisTime],[uSec],[threeR],[twoCV]
打印2:SELECT [thisDate],[thisTime],[uSec],[threeR],[twoCV] FROM [table_a] WHERE(thisDate =?AND thisTime>?)OR(thisDate>?),1900-09- 16,00:00
编辑:当我删除其中一列时,它似乎成功执行.尽管源表中存在两列.这没有回答关于原始查询不执行的原因的问题.
SELECT [Date], [Time], [uSec], [twoCV]
FROM [table_a]
WHERE (Date = ? AND Time > ?)
OR (Date > ?)
编辑2:更改日期和时间列的名称没有什么区别.以下仍然给出错误:
SELECT [thisDate], [thisTime], [uSec], [threeR], [twoCV]
FROM [table_a]
WHERE ([thisDate] = ? AND [thisTime] > ?)
OR ([thisDate] > ?)
[Microsoft] [ODBC Microsoft Access驱动程序]参数太少.预计5.
解决方法:
日期和时间为reserved words in Access,确保在查询中使用的任何位置转义保留字:
SELECT [Date], [Time], [uSec], [twoCV]
FROM [table_a] WHERE ([Date] = ? AND [Time] > ?)
OR ([Date] > ?)