【问题标题】:Error while passing arraylist as argument to INSERT data into Ignite cache using pyodbc使用pyodbc将arraylist作为参数传递给INSERT数据到Ignite缓存时出错
【发布时间】:2017-05-20 20:29:47
【问题描述】:

我编写了以下代码,使用 ODBC 驱动程序从 python 将数据插入 Ingite 缓存。在这个想要传递字符串数组列表(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')

【问题讨论】:

    标签: odbc pyodbc ignite


    【解决方案1】:

    根据文档,参数应作为单独的值或元组传递: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)
    

    【讨论】:

    • 嗨,您提到的链接指定通过一个接一个地获取列表中的元素来对给定列表执行 SQL 查询。在我的情况下,我想在 id 列中插入整个列表。
    • 通过上述修改,您提到我再次收到以下错误,Traceback (most recent call last): File "pythonOdbclist_new.py", line 13, in &lt;module&gt; cursor.executemany(select_string, id_list) pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')
    • 嗨@FlipperPA,我只想在一行中插入整个列表。谢谢。
    • 我认为 pyodbc 无法识别参数标记 '?' 即使我指定了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多