【问题标题】:Python cx_Oracle bind list of variablesPython cx_Oracle 绑定变量列表
【发布时间】:2018-08-15 09:17:20
【问题描述】:

如何将绑定参数与可变参数一起使用,例如,我将在 python 中动态准备一个 insert all 语句,并希望将其与数据行绑定。

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;

这可以在 python 上使用cx_oracle 吗?

【问题讨论】:

    标签: python oracle cx-oracle


    【解决方案1】:

    将您的真正问题解释为“如何在 cx_Oracle 中有效地插入大量数据”,答案是不要使用 INSERT ALL。相反,您应该使用 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)
    

    这在https://blogs.oracle.com/opal/efficient-and-scalable-batch-statement-execution-in-python-cx_oracle中都有描述

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 1970-01-01
      • 2021-06-16
      • 2011-11-02
      • 2018-01-07
      • 2020-09-14
      • 2021-12-17
      • 2016-10-19
      相关资源
      最近更新 更多