我编写了以下代码,使用ODBC驱动程序将数据从python插入到Ingite缓存中。在此想要传递字符串的arraylist(id_list)作为参数。 我的列表包含可变数量的元素,这就是我需要将其作为参数传递给查询的原因。
cursor = connection.cursor()
select_string= "INSERT INTO Person (_key, id, firstName, lastName, salary) VALUES (322, ? , 'abcd', 'dhsagd', 1000)"
id_list = ['test1', 'test2', 'test3', 'test4']
cursor.execute(select_string, id_list)
但是当我将字符串列表作为参数传递给查询时,我收到了以下错误。
Traceback (most recent call last):
File "pythonOdbclist.py", line 13, in <module>
cursor.execute(select_string, id_list)
pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 4 parameters were supplied', 'HY000')
答案 0 :(得分:0)
根据文档,参数应作为单个值或元组传递:https://github.com/mkleehammer/pyodbc/wiki/Cursor#executesql-parameters。您是否尝试在此示例中执行executemany
插入四行?
https://github.com/mkleehammer/pyodbc/wiki/Cursor#executemanysql-paras
cursor = connection.cursor()
select_string= "INSERT INTO Person (_key, id, firstName, lastName, salary) VALUES (322, ? , 'abcd', 'dhsagd', 1000)"
id_list = [('test1',), ('test2',), ('test3',), ('test4',)]
cursor.executemany(select_string, id_list)