如何将Python中的Oracle LOBs(CLOBBLOB)数据类型转换为String字符串?

2026-05-21 19:292阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计224个文字,预计阅读时间需要1分钟。

如何将Python中的Oracle LOBs(CLOB/BLOB)数据类型转换为String字符串?

从数据库直接读取小于1GB的CLOBs和BLOBs,将其格式化为字符串,比数据流方式更快。这里使用了`connection.outputtypehandler`:

pythondef OutputTypeHandler(cursor, name, defaultType, size, precision):


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

如何将Python中的Oracle LOBs(CLOB/BLOB)数据类型转换为String字符串?

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)

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


本文共计224个文字,预计阅读时间需要1分钟。

如何将Python中的Oracle LOBs(CLOB/BLOB)数据类型转换为String字符串?

从数据库直接读取小于1GB的CLOBs和BLOBs,将其格式化为字符串,比数据流方式更快。这里使用了`connection.outputtypehandler`:

pythondef OutputTypeHandler(cursor, name, defaultType, size, precision):


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

如何将Python中的Oracle LOBs(CLOB/BLOB)数据类型转换为String字符串?

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)

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