将arraylist作为参数传递给使用pyodbc将数据插入Ignite缓存时出错

时间:2017-05-20 20:29:48

标签: odbc pyodbc ignite

我编写了以下代码,使用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')

1 个答案:

答案 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)