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]
时间: 2025-01-29 22:32:41

Some tricks when using cx_Oracle的相关文章

Do not use LOB in Oracle(OLTP) -- record an optimization experience

Front knowledge LOB in Oracle LOB is used in Oracle to store text logger than 4000.  We don't use Oracle in a OLTP system.  Conside of RT and IO, we choose some other ways to provide log text. For example, CDN. LOB in cx_Oracle As mentioned in Some t

ASP.NET AJAX Advance Tips & Tricks系列文章目录

ASP.NET AJAX Advance Tips & Tricks(11) 三种方法动态创建T ASP.NET AJAX Advance Tips & Tricks (10) ASP.NET AJAX Advance Tips & Tricks (9) ASP.NET AJAX Advance Tips & Tricks (8) 扩展AJAX Control ASP.NET AJAX Advance Tips & Tricks (7) ASP.NET AJAX与U

【Python Oracle】使用cx_Oracle 进行数据库操作介绍

前面文章 <安装cx_Oracle 与使用><cx_Oracle 连接oracle的简单介绍> 介绍了基本使用,本文介绍一下使用python 对oracle 数据库进行常见操作的介绍 oracle@rac3:/home/oracle/python>cat sqlops.py  #!/usr/bin/python # -*- coding: utf-8 -*-  import cx_Oracle import sys import urllib import os # func

windows10,redhat6.5下python3.5.2使用cx_Oracle链接oracle

0.序言 项目主要使用oracle但是我不太喜欢其他编程语言,加上可能需要用python部署算法包,从oracle表中读出数据,处理完成后在放回oracle中去,所以在windows上就想到先用python试一下,自然搜到了cx_oracle(一个按照Python DB API的oracle的实现,如MySQL.PostgreSQL等,只需要安装相应的软件包即可,流程及操作接口都与cx_Oracle基本一致),下面就简单解释一下怎么用这个包进行增删改查. 1.windows 10 安装cx_Or

【Python Oracle】使用cx_Oracle 连接oracle的简单介绍

连接数据库的几种方式: 语法: cx_Oracle.connect('username','pwd','IP/HOSTNAME:PORT/TNSNAME') import cx_Oracle db1=cx_Oracle.connect('yang','yang','127.0.0.1:1523/yangdb') db2=cx_Oracle.connect('yang/yang@127.0.0.1:1523/yangdb') 对于dsn 方式: makedsn(IP/HOST,PORT,TNSNA

windows下python3 使用cx_Oracle,xlrd插件进行excel数据清洗录入

我们在做数据分析,清洗的过程中,很多时候会面对各种各样的数据源,要针对不同的数据源进行清洗,入库的工作.当然python这个语言,我比较喜欢,开发效率高,基本上怎么写都能运行,而且安装配置简单,基本上有网的环境pip install全部都搞定,没网的话,把whl包copy过来一行命令也就解决了(windows下python3.5使用pip离线安装whl包). 本篇博客就针对,在windows平台下使用python3(python2社区将要停止支持,使用3是大势所趋),读取xls,xlsx格式的数

Android 6.0 Marshmallow tips and tricks 棉花糖的技巧和窍门

Since you're reading these lines, chances are you're among the lucky 2 percent of Android users who happen to have Marshmallow running on their smartphone. You've either received an update to Android 6.0, or you're simply a Nexus user.从你看到这个起,你很幸运的了解

python cx_Oracle静态编译安装

cx_Oracle是python连Oracle常用的lib,今天介绍如何使用静态链接的方式build cx_Oracle,避免对oracle共享库的依赖. 首先需要生成oracle客户端的静态lib: cd $ORACLE_HOME/bin ./genclntst ar: creating /u01/app/oracle/product/11.2.0/db_1/lib/libclntst11.a Created /u01/app/oracle/product/11.2.0/db_1/lib/li

linux下运行python导入包cx_oracle报错ELFCLASS32

问题描述 linux下运行python导入包cx_oracle报错ELFCLASS32 linux运行python导入oracle包报错ELFCLASS32,linux平台是64bit,python也是64bitcx_Oracle网上下载应该不区分多少位吧?问题:这个报错是如何解决?请用linux和python的大神进 解决方案 你要看下 cx_oracle 是不是支持2.6.6版本,有可能不支持. 解决方案二: 参考:http://www.educity.cn/wenda/288024.htm