如何使用 cx_oracle python lib 执行包含分号的 DML 语句

How to execute DML statements that contain semi colons using cx_oracle python lib

目前我使用 cursor.execute() 方法执行多个 DML 语句。我使用 ; 拆分语句分隔符并将每个语句传递给 cursor.execute() 方法。在我遇到一些在 SQL 语句本身中包含分号的语句之前,这种方法一直有效。

我尝试使用 cursor.executemany() 方法,通过传递多个语句而不用分号分隔。但是它会抛出错误来声明参数值。但是我执行的所有语句都是静态的,我不需要任何序列或迭代。

cursor.executemany(sqlCommands,1,batcherrors=True)

TypeError: 需要字符串、unicode 或缓冲区对象

cursor.executemany(sqlCommands,'',batcherrors=True)

TypeError: 参数应该是一个sequences/dictionaries的列表或者一个指定语句执行次数的整数

如何处理 SQL 语句中的分号以使用 cx_oracle python lib

执行

您必须删除结尾的分号。它们不是 SQL 语句的一部分,并且 Oracle 数据库不接受它们 - 如您所见。分号是一些工具(如 SQL*Plus)的指示符,用于将任何前面的文本发送到数据库。

如何删除分号取决于您的操作。最好限制您的输入文件语法,让您的生活更轻松。示例设置脚本 SampleEnv.py 之所以有效,是因为它要求输入文件语句有尾部斜杠。

cx_Oracle 的 executemany() 函数使用 many 数据值执行 one 语句。它不执行多个语句。参考文档 Batch Statement Execution and Bulk Loading.