PypyODBC搭配参数:[ODBC Microsoft Access Driver] 参数不足。预期4个。

2024-07-05 10:00:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

编辑3:这是从中提取表的设计视图。 enter image description here


Tags: orandthefrom参数dateaccesstime
3条回答

另请补充布莱恩·埃尔格尔的回答:我刚刚遇到了这个问题。结果只是列名有误。

当我故意使用两个错误的列名时,会出现以下错误:

Traceback (most recent call last):
  File "C:\...\some_code.py", line 74, in <module>
    table_headers, table_data = fetch_relations()
  File "C:\...\some_code.py", line 27, in fetch_relations
    cur.execute(sql);
  File "C:\Python34\lib\site-packages\pypyodbc.py", line 1605, in execute
    self.execdirect(query_string)
  File "C:\Python34\lib\site-packages\pypyodbc.py", line 1631, in execdirect
    check_success(self, ret)
  File "C:\Python34\lib\site-packages\pypyodbc.py", line 986, in check_success
    ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
  File "C:\Python34\lib\site-packages\pypyodbc.py", line 966, in ctrl_err
    raise DatabaseError(state,err_text)
pypyodbc.DatabaseError: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.')

注意,在最后一行,Expected 2.与有问题的列的数量对齐。对我来说,当列名正确时,问题就消失了。在

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

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

错误源于第一个游标查询,因为正如您在table design view中显示的那样(除非屏幕截图中截取了更多字段),在table_a中没有名为[Datetime]的列:

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

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

^{pr2}$

相关问题 更多 >