使用 cx_Oracle 批量插入:不一致的数据类型

Bulk insert with cx_Oracle: inconsistent data types

我正在尝试使用 Python 和 cx_Oracle 将大量记录加载到 Oracle 数据库中。共识似乎是您应该针对行列表准备游标和 executemany(每个 this post)。所以我的代码看起来像:

stmt = "INSERT INTO table (address, shape) VALUES (:1, :2)"
cursor.prepare(stmt)
rows = []
# Make huge list of rows
cursor.executemany(None, rows)

我传递的值如下所示:

['1234 MARKET ST', "SDE.ST_Geometry('POINT (0 0)', 2272)"]

问题是 SDE.ST_Geometry() 数据库函数被视为文字字符串而不是被求值,所以我得到 cx_Oracle.DatabaseError: ORA-00932: inconsistent datatypes: expected SDE.ST_GEOMETRY got CHAR.

是否无法使用 cx_Oracle 将数据库函数传递给准备好的游标?

简短的回答是,由于您传递的是字符串,因此它被视为字符串。绑定值仅被视为数据。

但是看看还没发布的cx_Oracle主线https://bitbucket.org/anthony_tuininga/cx_oracle?它有新的对象支持。

并查看此提交 "Added example for creating SDO_GEOMETRY.": https://bitbucket.org/anthony_tuininga/cx_oracle/commits/2672c799d987a8901ac1c4917e87ae4101a1d605

最后我通过在准备好的语句中嵌入函数调用解决了这个问题:

stmt = "INSERT INTO table (address, shape) VALUES (:1, ST_Geometry(:2, 2272))"

向 PatrickMarchand 的提示致敬。