简体   繁体   English

TypeError using Python to insert multiple rows with cx_Oracle

[英]TypeError using Python to insert multiple rows with cx_Oracle

Below is the code giving the following error:下面是给出以下错误的代码:

TypeError: parameters should be a list of sequences/dictionaries or an integer specifying the number of times to execute the statement类型错误:参数应该是序列/字典列表或 integer 指定执行语句的次数

sql=("SELECT * FROM ABCD WHERE SA in ('111111111')")
cur_src_qa = conn_src_qa.cursor()
cur_src_qa.execute(sql)
df_s = cur_src_qa.fetchall()
cur_src_qa.close()   
cur_src_dev = conn_src_dev.cursor()
cur_src_dev.execute("delete ABCD where SA in ('111111111')")
conn_src_dev.commit()
cur_src_dev.executemany("insert into ABCD (A, B, C, D, E, F, F, G, H, I, J) values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", df_s[0])
# cur_src_dev.executemany("insert into ABCD values ({​​​​​}​​​​​)", df_s)
conn_src_dev.commit()
cur_src_dev.close()

Start by replacing the %s with bind variable syntax.首先用绑定变量语法替换 %s。 Then get your data in the right input format - in your example just use the query return variable directly.然后以正确的输入格式获取您的数据 - 在您的示例中,只需直接使用查询返回变量。

Given this table:鉴于此表:

SQL> create table test (k number, v varchar2(20));

Table created.

SQL> insert into test (k,v) values (1, 'abc');

1 row created.

SQL> insert into test (k,v) values (2, 'def');

1 row created.

SQL> insert into test (k,v) values (3, 'ghk');

1 row created.

SQL> commit;

Commit complete.

And the Python code:和 Python 代码:

    cursor.execute("select k,v from test")
    rows = cursor.fetchall()
    print(rows);

    cursor.execute("delete from test")

    cursor.executemany("insert into test (k,v) values (:1, :2)", rows)
    
    cursor.execute("select k,v from test")
    rows = cursor.fetchall()
    print(rows);

The output is: output 是:

$ python so12.py 
[(1, 'abc'), (2, 'def'), (3, 'ghk')]
[(1, 'abc'), (2, 'def'), (3, 'ghk')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM