Python访问Oracle, 首选cx_Oracle模块, 功能十分强劲!


#==========================

# 下载cx_Oracle源码

#==========================

cx-oracle官网

    http://cx-oracle.sourceforge.net/

如果选择用源码安装, 下载最新版的Source Code only 包即可. 如果oracle 客户端是Oracle instant client, 除了安装basic package外, 还需要安装sdk包, 否则cx_oracle无法编译.


#==========================

# 下载cx_Oracle 编译包

#==========================

如果要使用编译好的包, 选择什么版本就能讲究了. 如果你的机器已经安装好Oracle客户端了, 先要看一下看操作系统的是x64还是i386架构, 再看机器上安装的Oracle客户端的版本(10g还是11g, 64bit还是32bit), 最后再看你的python版本(2.7还是2.6, 64bit还是32bit). cx_Oracle版本一定要选对. 否则即使安装完也无法使用. 

python --version

如何确定python是32bit还是64bit, 在windows下很容易, 进入python shell后, 从提示信息就能看出. 在linux下, 需要使用file 命令, 比如 file usr/bin/python2.5


#==========================

# 在Windows上安装 Oracle Instant Client

#==========================

摸索出 Windows上cx-Oracle安装过程确实不易, 看来很少有人在windows 上玩 cx_Oracle.

参考文章:

 http://gypsyer.blog.51cto.com/734537/163344

 http://mostperfect.net/blog/2010/07/28/installing-cx_oracle-on-windows/


1. 下载并安装 Oracle Instant Client 的 Basic package, 其它包可选,sdk包推荐安装,  比如安装目录为 C:\Oracle\instantclient,  (官网地址:

http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

镜像地址: http://eduunix.ccut.edu.cn/index2/database/Oracle%20Instant%20Client/ )

2. 为 instantclient 增加 tns 文件. Oracle Instant Client 安装后并没有tns文件, 需要自己创建配置tns文件, 按照oracle的命名惯例, 在 C:\Oracle\instantclient 下创建 network\admin 目录, 然后放置一个 tnsnames.ora 文件.

3. 增加环境变量 Path, 将 C:\Oracle\instantclient 增加进去.

4. 增加环境变量 TNS_ADMIN , 指向 上面的 TNS 文件夹.  如果 tnsnames.ora  文件没有放在C:\Oracle\instantclient\network\admin, 这步是必需的.

5. 修改注册表 NLS_LANG 值, \HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE 查找键 NLS_LANG, 这个键由Oracle标准客户端安装创建, 值是 NA 。这个导致了 ORA-12705错误。解决方法就是修改NA为 SIMPLIFIED CHINESE_CHINA.ZHS16GBK 。

#==========================

# 在Windows上配置 cx_oracle

#==========================

6. 将C:\Oracle\instantclient的所有dll 文件, 复制到 {pythonInstall}\Lib\site-packages目录下

7. 根据 python 的版本和 Oracle 客户端的版本, 下载合适的 cx_Oracle 编译包. (http://cx-oracle.sourceforge.net/). 安装之.

8. 测试 cx_Oracle

import cx_Oracle

db_conn = cx_Oracle.connect(“user/pass@tnsname.world”)   


如果 import cx_Oracle  报错ImportError: DLL load failed: 找不到指定的程序。 可以使用Dependency Walker这个工具打开 cx_Oracle.pyd, 看看到底还缺哪些dll

   Dependency Walker下载地址: http://www.dependencywalker.com/


如果实在安装不上编译版, 试试源码编译安装吧, 过程也很痛苦, 参考: How to compile cx_Oracle (python 3.2 for Windows x64)

http://www.dbaportal.eu/?q=node/194


#==========================

# 在Linux安装前的配置,

#==========================

参考文章:

 http://cx-oracle.sourceforge.net/BUILD.txt

 在.bash_profile中,

   export ORACLE_HOME=[your installation path]/instantclient_11_1

   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

   export NLS_LANG=.UTF8 # 否则会报 ORA-xxx错误

   另外, 建立libclntsh.so symlink  

   cd $ORACLE_HOME

   find .|grep libclntsh, 找到libclntsh.so.x.x,

   ln -s libclntsh.so.x.x libclntsh.so




#==========================

# 在Linux上源码安装,

#==========================

在Linux上, 推荐使用源码安装.

    python setup.py build

    python setup.py install --prefix=/usr/local



#==========================

# 在Linux上RPM安装,

#==========================

如果有root账号, 可以直接安装cx_Oracle的rpm包. 我的linux上已经安装了多个python版本, 用这种方法安装, cx_Oracle始终不能被import. 可能的解决方法是: 在运行rpm时候, 通过--prefix选项来指定软件包安装的路径  

rpm -ivh cx_Oracle-5.1-10g-py27-1.x86_64.rpm

或rpm -ivh --nodeps cx_Oracle-5.1-10g-py27-1.x86_64.rpm




#==========================

# cx-oracle的教程    ,

#==========================    

    [简单] http://www.orafaq.com/wiki/Python

    [简单] http://codingtutorials.co.uk/blog/?p=31

    [全面]Sidekick - cx_Oracle (code paterns)系列 http://www.dbaportal.eu/?q=node/125

    [全面] http://www.oracle.com/technetwork/articles/dsl/python-091105.html

    [示例] http://code.google.com/p/cx-oracle-demos

    [介绍]Python cx_Oracle 5.0新特性 http://www.oszx.net/archives/718

    [注意]如果是sqlalchemy+cx_oracle的话, 需要禁掉 connection pool, 否则会有异常. 方法是设置sqlalchemy.poolclass为sqlalchemy.pool.NullPool


如何执行Oracle的存储过程, 并取到存储过程的out游标

http://stackoverflow.com/questions/6821372/python-oracle-passing-in-a-cursor-out-parameter

--PL SQL
create or replace procedure sp_procedure(
cid int,
rep_date date,
ret out sys_refcursor
) is
begin
open ret for
select
...
end;


#python
import cx_Oracle
from datetime import date

connstr='user/pass@127.0.0.1:2521/XE'
connstr='user/pwd@tns'

conn = cx_Oracle.connect(connstr)
cursor = conn.cursor()
cid = 1
rep_date = date(2011,06,30)
l_cur = cursor.var(cx_Oracle.CURSOR)
l_query = cursor.callproc('sp_procedure', (cid,rep_date,l_cur))
l_results = l_query[2]
for row in l_results:
print row
# Column Specs
for row in l_results.description:
print row