4

我正在使用 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:这是它从中提取的表的设计视图。 在此处输入图像描述

4

3 回答 3

2

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

于 2015-11-01T12:38:14.430 回答
1

Date并且TimeAccess中的保留字,请确保在查询中使用的任何地方都对保留字进行转义:

SELECT [Date], [Time], [uSec], [twoCV] 
FROM [table_a] WHERE ([Date] = ? AND [Time] > ?) 
OR ([Date] > ?)
于 2015-10-12T15:01:02.853 回答
1

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

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() 
于 2015-11-01T15:59:10.593 回答