gpt4 book ai didi

python - 参数为 : [ODBC Microsoft Access Driver] Too few parameters. 的 PypyODBC 预期为 4

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:46 24 4
gpt4 key购买 nike

我正在使用 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:这是它从中提取的表的设计 View 。 enter image description here

最佳答案

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

关于python - 参数为 : [ODBC Microsoft Access Driver] Too few parameters. 的 PypyODBC 预期为 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33083827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com