Sandip Acharya
Sandip Acharya

Reputation: 11

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

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()

Upvotes: 1

Views: 3368

Answers (1)

Christopher Jones
Christopher Jones

Reputation: 10506

Start by replacing the %s with bind variable syntax. 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:

    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:

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

Upvotes: 1

Related Questions