PypyODBC参数:[ODBC Microsoft Access Driver]参数太少。预计4

时间:2015-10-12 14:36:34

标签: python sql odbc pyodbc pypyodbc

我使用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

编辑:当我删除其中一列时,似乎成功执行了它。尽管源表中存在两列。这并没有回答原始查询未执行的原因。

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。

编辑3 :这是从中拉出的表格的设计视图。 enter image description here

3 个答案:

答案 0 :(得分:1)

DateTimereserved words in Access,请确保在查询中使用的任何位置转义保留字:

SELECT [Date], [Time], [uSec], [twoCV] 
FROM [table_a] WHERE ([Date] = ? AND [Time] > ?) 
OR ([Date] > ?)

答案 1 :(得分:1)

过于补充Bryan Eargle的回答:我刚才遇到过这个问题。 结果发现列名错误。

当我故意使用两个错误的列名时,我收到以下错误:

Expected 2.

请注意,在最后一行中,t = 0.5*(x + 1)*(b - a) + a 与有问题的列数对齐。对我来说,当列名正确时,问题就会消失。

答案 2 :(得分:1)

错误来自第一个游标查询,因为当您使用表格设计视图显示时(除非屏幕截图中有更多字段),[Datetime]中没有名为table_a的列:< / p>

for row in curSQL.execute('SELECT MAX(Datetime) FROM [' + _.getName() + ']'):

考虑更改字段以反映新列名称。此外,要使用strptime(),原始变量row[0]必须是字符串,因此您可能会收到Python TypeError。 Jet / ACE datetime字段将作为datetime字段导入到Python中,因此无需转换:

for row in curSQL.execute('SELECT MAX(thisDate) FROM [' + _.getName() + ']'):
    xDateTime = row[0]
    day = xDateTime.date() 
    time = xDateTime.time() 
相关问题