我正在使用 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 Driver] 参数太少。预期为 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: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 Driver] 参数太少。预计5。