con = cx_Oracle.connect(connection_string)
cur = con.cursor()
cur.execute("SET DEFINE OFF")
抛出错误:
DatabaseError追溯(最近一次通话)在
1 con = cx_Oracle.connect(connection_string)
2 cur = con.cursor()
3 cur.execute("SET DEFINE OFF")
DatabaseError:ORA-00922:缺少或无效的选项
如注释中所述,SET DEFINE是SQL * Plus命令,并且cx_Oracle不会解释。您收到ORA错误,因为cx_Oracle将语句发送到数据库,并且数据库仅处理SQL或PL / SQL语句。
您应该使用绑定变量而不是(不可用)替换变量。任何类型的字符串替换或插值都是可伸缩性问题和安全风险。绑定变量解决了这些问题。 (可以说您也可以在SQL * Plus中使用绑定变量)。
查看https://github.com/oracle/python-cx_Oracle/blob/master/samples/BindInsert.py之类的示例
rows = [ (1, "First" ),
(2, "Second" ),
(3, "Third" ),
(4, "Fourth" ),
(5, "Fifth" ),
(6, "Sixth" ),
(7, "Seventh" ) ]
cursor = connection.cursor()
cursor.executemany("insert into mytab(id, data) values (:1, :2)", rows)
检查cx_Oracle文档,例如https://cx-oracle.readthedocs.io/en/latest/user_guide/sql_execution.html#insert-and-update-statements和https://cx-oracle.readthedocs.io/en/latest/user_guide/bind.html#bind和https://cx-oracle.readthedocs.io/en/latest/user_guide/batch_statement.html#batchstmnt