Some tricks when using cx_Oracle

简介:

cx_Oracle is a Python module that enables access to Oracle databases.
However, users may have confusion due to some fetures of this module.

Thread safe

The default setting of cx_Oracle is not thread-safe. 
So if user have multiple threads, make sure that specifying threaded=True when creating the connection.

conn = cx_Oracle.connect(user + '/' + passwd + "@" + host + "/" + db, threaded=True)

Otherwise, the program will crash with error message like

ORA-24550: signal received: [si_signo=11] [si_errno=0] [si_code=2] [si_addr=0000000000000000]

Fetch LOB column

Internally, Oracle uses LOB locators which are allocated based on the cursor array size. Thus, it is important that the data in the LOB object be manipulated before another internal fetch takes place. The safest way to do this is to use the cursor as an iterator. In particular, do not use the fetchall() method. The exception “LOB variable no longer valid after subsequent fetch” will be raised if an attempt to access a LOB variable after a subsequent fetch is detected.

Use curosr as an iterator rather than use fetchall() method.

self._cursor.execute(sql, *args)
def fix_lob(row):
    def convert(col):
        if isinstance(col, cx_Oracle.LOB):
            return str(col)
        else:
            return col

    return [convert(c) for c in row]

return [fix_lob(r) for r in self._cursor]

目录
相关文章
|
SQL Oracle 关系型数据库
Oracle 中 Translate的用法
Oracle 中 Translate的用法
108 0
|
Oracle 关系型数据库
【ogg一】入门OGG(oracle golden date)详细部署 Oracle 11g
【ogg一】入门OGG(oracle golden date)详细部署 Oracle 11g
230 0
|
Oracle 关系型数据库 Perl
oracle grid p28429134 psu 安装
181016 PSU 安装,p28429134_112040_Linux-x86-64.zip psu 安装。
3059 0
Oracle APEX 系列文章10:Oracle APEX Evangelion(EVA 补完计划)
Neon Genesis Evangelion(EVA) is one of my favorite cartoons, I use Evangelion as the title to represent my respect for EVA.
3435 0
|
Oracle 关系型数据库 数据库