“Optional feature not implemented (106) (SQLBindParameter)” error with pyodbc

旧时模样 提交于 2019-11-29 11:41:41

You've got an extra comma in your parameters list that is messing you up. The following code works for me under Python 2.7:

import pyodbc
sname = 'Gord'
rcount = 3
cnxn = pyodbc.connect(
        'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
        'DBQ=C:\\Users\\Public\\Database1.accdb;')
cursor = cnxn.cursor()
sql = "insert into core_data(screen_name,retweet_count) values (?,?)"
params = (sname, int(rcount))
cursor.execute(sql, params)
cursor.commit()
cnxn.close()

Edit:

It turns out that there is a reported issue with integer parameters when pyodbc interacts with Access ODBC while running under under Python 3.x. One possible workaround would be to try downloading and installing pypyodbc and then trying this code (which works for me under Python 3.5.2):

import pypyodbc
sname = 'Gord'
rcount = 3
cnxn = pypyodbc.connect(
        'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
        'DBQ=C:\\Users\\Public\\Database1.accdb;')
cursor = cnxn.cursor()
sql = "insert into core_data(screen_name,retweet_count) values (?,?)"
params = (sname, rcount)
cursor.execute(sql, params)
cursor.commit()
cnxn.close()
Adrien

I had the same error with this driver and pyodbc, it turned out that converting integers to floats solved the issue:

row = [1, 2, "foo"]
row = [float(x) if type(x) is int else x for x in row] #Convert all int items to floats
cursor.execute("insert into table1 (a,b,c) values (?,?,?)",row).commit()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!