当前位置 : 主页 > 编程语言 > python >

Python转Oracle LOBs(CLOB/BLOB) 为String字符串

来源:互联网 收集:自由互联 发布时间:2022-07-13
从数据库直接读取小于1GB的CLOBs and BLOBs的格式作为字符串,这比数据流方式更快。 这里用到了​​​connection.outputtypehandler​​: def OutputTypeHandler ( cursor , name , defaultType , size , precisio


从数据库直接读取小于1GB的CLOBs and BLOBs的格式作为字符串,这比数据流方式更快。
这里用到了​​​connection.outputtypehandler​​:

def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
if defaultType == cx_Oracle.DB_TYPE_CLOB:
return cursor.var(cx_Oracle.DB_TYPE_LONG, arraysize=cursor.arraysize)
if defaultType == cx_Oracle.DB_TYPE_BLOB:
return cursor.var(cx_Oracle.DB_TYPE_LONG_RAW, arraysize=cursor.arraysize)

idVal = 1
textData = "The quick brown fox jumps over the lazy dog"
bytesData = b"Some binary data"
cursor.execute("insert into lob_tbl (id, c, b) values (:1, :2, :3)",
[idVal, textData, bytesData])

connection.outputtypehandler = OutputTypeHandler
cursor.execute("select c, b from lob_tbl where id = :1", [idVal])
clobData, blobData = cursor.fetchone()
print("CLOB length:", len(clobData))
print("CLOB data:", clobData)
print("BLOB length:", len(blobData))
print("BLOB data:", blobData)

参考:https://cx-oracle.readthedocs.io/en/latest/user_guide/lob_data.html#lobdata


上一篇:Python3字典转get请求参数拼接
下一篇:没有了
网友评论