hangc
hangc

Reputation: 5473

Python cx_Oracle bind list of variables

How can I use bind parameters with variable parameters, for example I would prepare in python dynamically an insert all statement, and would like to bind it with rows of data.

INSERT ALL
  INTO mytable (column1, column2, column_n) VALUES (:expr[0][0], :expr[0][1], :expr[0][n])
  INTO mytable (column1, column2, column_n) VALUES (:expr[1][0], :expr[1][n], :expr[1][n])
  INTO mytable (column1, column2, column_n) VALUES (:expr[n][0], :expr[n][1], :expr[n][n])
SELECT * FROM dual;

Is this possible using cx_oracle on python?

Upvotes: 0

Views: 1605

Answers (1)

Christopher Jones
Christopher Jones

Reputation: 10506

Interpreting your real question as 'how do I insert lots of data efficiently in cx_Oracle', the answer is not to use INSERT ALL. Instead you should use executemany():

data = [
    (60, "Parent 60"),
    (70, "Parent 70"),
    (80, "Parent 80"),
    (90, "Parent 90"),
    (100, "Parent 100")
]

cursor.executemany("""
        insert into ParentTable (ParentId, Description)
        values (:1, :2)""", data)

This is all described in https://blogs.oracle.com/opal/efficient-and-scalable-batch-statement-execution-in-python-cx_oracle

Upvotes: 2

Related Questions