我使用pypyodbc从access数据库中选择数据。我使用下面的查询,其中有三个指定的参数。在
我试过几种品种,但没有效果。我看不出我的语法有什么问题。在
SELECT [Date], [Time], [uSec], [threeR], [twoCV]
FROM [table_a]
WHERE (Date = ? AND Time > ?)
OR (Date > ?)
参数类型如下:
^{pr2}$打印时:
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:[此日期],[此时间],[使用],[三人],[twoCV]
打印2:从[表a]中选择[此日期]、[此时间]、[uSec]、[三人]、[两人]其中(thisDate=?而这次呢或(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。
另请补充布莱恩·埃尔格尔的回答:我刚刚遇到了这个问题。结果只是列名有误。
当我故意使用两个错误的列名时,会出现以下错误:
注意,在最后一行,
Expected 2.
与有问题的列的数量对齐。对我来说,当列名正确时,问题就消失了。在Date
和Time
是reserved words in Access,请确保在查询中使用的任何位置对保留字进行转义:错误源于第一个游标查询,因为正如您在table design view中显示的那样(除非屏幕截图中截取了更多字段),在
table_a
中没有名为[Datetime]
的列:考虑更改字段以反映新的列名。另外,要使用
^{pr2}$strptime()
,原始变量row[0]
必须是字符串,因此您可能会收到一个PythonTypeError
。Jet/ACE日期时间字段将作为日期时间字段导入到Python中,因此不需要转换:相关问题 更多 >
编程相关推荐