我正在try 使用Python(3.11)和oracledb库(1.3.0)从Oracle数据库中获取SDO_GEOMETRY类型的列.我想使用outputtypeHandler将SDO_GEOMETRY实例转换为PICKLE编码的字节.如果我try 将cursor.var
中的typ
参数设置为typ=str
,这对于数字列很有效,但对于所有类型的列类型,这对于typ=bytes
和typ=oracledb.DB_TYPE_RAW
都失败了.无论参数值为typ
,SDO_GEOMETRY列始终会产生错误.输出转换器甚至不会被调用,如下所示.
以下是我的示例代码:
import oracledb
import pickle
def output_type_handler(cursor, name, default_type, size, precision, scale):
def pickle_converter(obj) -> bytes:
print(f"Converter called for {name}.")
return pickle.dumps(obj)
if default_type == oracledb.DB_TYPE_OBJECT:
return cursor.var(
typ=oracledb.DB_TYPE_RAW,
size=size,
arraysize=cursor.arraysize,
outconverter=pickle_converter
)
# Switch to thick mode
oracledb.init_oracle_client()
ora_connection = oracledb.connect(
dsn=oracledb.makedsn("ora.local", 1521, "TST"),
user="test",
password="test"
)
ora_connection.outputtypehandler = output_type_handler
with ora_connection.cursor() as cursor:
# GEOMETRIE is an SDO_GEOMETRY column
recs = cursor.execute("SELECT GEOMETRIE FROM MV_CS_STWG1KP").fetchmany(5)
print(recs)
OUTPUT(请注意,行Converter called for ...
甚至没有打印,因此从未调用转换器):
Traceback (most recent call last):
File "/home/jannis/.config/JetBrains/PyCharmCE2023.1/scratches/tmp.py", line 28, in <module>
num_recs = cursor.execute("SELECT GEOMETRIE FROM MV_CS_STWG1KP").fetchmany(5)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jannis/PycharmProjects/etl_engine/venv/lib/python3.11/site-packages/oracledb/cursor.py", line 492, in fetchmany
row = fetch_next_row(self)
^^^^^^^^^^^^^^^^^^^^
File "src/oracledb/impl/base/cursor.pyx", line 397, in oracledb.base_impl.BaseCursorImpl.fetch_next_row
File "src/oracledb/impl/thick/cursor.pyx", line 132, in oracledb.thick_impl.ThickCursorImpl._fetch_rows
File "src/oracledb/impl/thick/utils.pyx", line 413, in oracledb.thick_impl._raise_from_odpi
File "src/oracledb/impl/thick/utils.pyx", line 403, in oracledb.thick_impl._raise_from_info
oracledb.exceptions.DatabaseError: ORA-00932: inconsistent datatypes: expected BINARY got ADT
我必须使用胖模式才能连接到较旧的Oracle数据库. 我怎么才能解决这个问题?