【问题标题】:Cx_Oracle OutputTypeHandler for CLOB/NCLOB ErrorCLOB/NCLOB 错误的 Cx_Oracle OutputTypeHandler
【发布时间】:2020-09-11 20:12:03
【问题描述】:

我正在使用 cx_Oracle for Python 7.3.0 从 Oracle 数据库表中下载数据。 我注意到,每当我下载和处理一些包含 CLOB/NCLOB 数据的表时,我的速度都会大大降低。

如此处所示:https://cx-oracle.readthedocs.io/en/latest/user_guide/lob_data.html#fetching-lobs-as-strings-and-bytes 我想尝试以字符串和字节的形式获取 LOB。

我认为我所做的一切都是正确的,但代码仍然抛出错误。我还检查了使用 OutputTypeHandler 更改其他类型(即数字类型),并尝试在连接级别而不是光标级别进行更改 - 没有一个有效。

代码:

import cx_Oracle
import csv
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_NCLOB:
        return cursor.var(cx_Oracle.DB_TYPE_LONG, arraysize=cursor.arraysize)

ip=1.2.3.4
port=8888
SID='DB1'
dsn_tns=cx_Oracle.makedsn(ip,port,SID)

db=cx_Oracle.connect('USER', 'PASS',dsn_tns)
cursor=db.cursor()
cursor.arraysize=500
cursor.outputtypehandler = OutputTypeHandler
cursor.execute("SELECT * FROM dual").fetchall() #or any other table, of course
db.close()

错误信息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-106-bbcd7561378d> in <module>
     16 cursor.arraysize=500
     17 cursor.outputtypehandler = OutputTypeHandler
---> 18 cursor.execute("SELECT * FROM dual").fetchall()
     19 db.close()
     20 

<ipython-input-106-bbcd7561378d> in OutputTypeHandler(cursor, name, defaultType, size, precision, scale)
      2 import csv
      3 def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
----> 4     if defaultType == cx_Oracle.DB_TYPE_CLOB:
      5         return cursor.var(cx_Oracle.DB_TYPE_LONG, arraysize=cursor.arraysize)
      6     if defaultType == cx_Oracle.DB_TYPE_NCLOB:

AttributeError: module 'cx_Oracle' has no attribute 'DB_TYPE_CLOB'

【问题讨论】:

    标签: python python-3.x oracle cx-oracle


    【解决方案1】:

    cx_Oracle 8 添加了一些新常量。

    查看cx_Oracle 7.3 doc,您会使用较旧的名称,例如:

    def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
        if defaultType == cx_Oracle.CLOB:
            return cursor.var(cx_Oracle.LONG_STRING, arraysize=cursor.arraysize)
        if defaultType == cx_Oracle.BLOB:
            return cursor.var(cx_Oracle.LONG_BINARY, arraysize=cursor.arraysize)
    

    【讨论】:

      猜你喜欢
      • 2011-10-01
      • 2011-10-14
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2011-09-02
      • 1970-01-01
      相关资源
      最近更新 更多