Python executemany函数

时间:2015-01-14 11:44:29

标签: python sql oracle executemany

使用cx_oracle

运行executemany时遇到问题

当我运行以下声明时,我收到ORA-01036: illeagal variablename/number

infotext_list是应与"SOMETHING"进行比较的字符串列表 看起来像[“abc”,“bcd”,“def”,...],其中的每个字符串都应与其他数据表中的SOMETHING进行比较!

insert_stmt = 'INSERT INTO data_table (...) SELECT ... FROM other_table WHERE SOMETHING = ? '
curs.executemany(insert_stmt, infotext_list)

如果我遍历infotext_list并使用标准的execute()方法,它就可以正常工作,但需要永远。

1 个答案:

答案 0 :(得分:0)

我改变了几件让我为此工作的事情。

1-将infotext_list更改为mappings or sequences

的列表
infotext_list = [("abc",), ("bcd",), ("def",)] --notice the extra , inside the ()

infotext_list = [{'value':"abc"}, {'value':"bcd"}, {'value':"def"}]

2-变化? to:如果你想使用一个映射,则为value;如果你想使用序列,则为:1(或任何其他:你想要的名称)

这两项都适合我

infotext_list = [("abc",), ("bcd",), ("def",)]
insert_stmt = 'INSERT INTO data_table (...) SELECT ... FROM other_table WHERE SOMETHING = :1 '
curs.executemany(insert_stmt, infotext_list)

infotext_list = [{'value':"abc"}, {'value':"bcd"}, {'value':"def"}]
insert_stmt = 'INSERT INTO data_table (...) SELECT ... FROM other_table WHERE SOMETHING = :value '
curs.executemany(insert_stmt, infotext_list)

我简要介绍了如何使用cx_Oracle进行crud操作here