【问题标题】:PypyODBC with parameters: [ODBC Microsoft Access Driver] Too few parameters. Expected 4带参数的 PypyODBC:[ODBC Microsoft Access 驱动程序] 参数太少。预计 4
【发布时间】:2016-01-10 02:12:06
【问题描述】:

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

编辑 3:这是从中提取的表格的设计视图。

【问题讨论】:

标签: python sql odbc pyodbc pypyodbc


【解决方案1】:

太补充了 Bryan Eargle 的回答:我刚才遇到了这个。 原来是列名错误。

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

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. 与有问题的列数对齐。对我来说,当列名正确时问题就消失了。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 以为这是解决方案,但是在更改了日期和时间的列之后(请参阅编辑),我仍然遇到错误。但是,它现在需要 5 个参数而不是 4 个(?)
    【解决方案3】:

    错误来自第一个游标查询,因为正如您在表格设计视图中显示的那样(除非您的屏幕截图截断了更多字段),table_a 中没有名为 [Datetime] 的此类列:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 2021-11-28
      相关资源
      最近更新 更多