python安装oracle扩展及数据库连接方法

本文实例讲述了python安装oracle扩展及数据库连接方法。分享给大家供大家参考,具体如下:

下载:

cx_oracle下载地址:http://cx-oracle.sourceforge.net/instantclient-basic

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

window环境:

python27 oracle10

需要软件:

cx_oracle-5.1.2-10g.win32-py2.7.msiinstantclient-basic-win32-10.2.0.4.zip

1. 直接双击msi文件,即安装cx_oracle;

2. 解压instantclient-basic-win32-10.2.0.4.zip,将得到的.dll文件全部拷贝到f:\python27\lib\site-packages目录下

linux环境:

python26 orracle10

需要软件:

cx_oracle-5.1.2-10g-py26-1.x86_64.rpmbasic-10.2.0.4.0-linux-x86_64.zip

1. rpm -ivh cx_oracle-5.1.2-10g-py26-1.x86_64.rpm2. (此处参考#)

设置环境变量

vi /root/.bash_profile

增加如下两行:

export oracle_home=/usr/local/instantclient_10_2
export ld_library_path=$ld_library_path:$oracle_home

运行source /root/.bash_profile使改动生效

建立此链接库的符号链接

cd $oracle_home
ln -s libclntsh.so.x.x libclntsh.so

重新安装cx_oracle

注意加–nodeps参数,否则还会报上述错误

[root@bj-update-01 ~]# rpm -ivh –nodeps cx_oracle-5.0.1-10g-py24-1.x86_64.rpm#5.0.3版本不用加–nodeps参数

测试:

#python
>>> import cx_oracle
>>> db = cx_oracle.connect(‘user/psw@114.242.113.91:1521/orcl’)
>>> print db

>>> cr=db.cursor()
>>> cr.execute(“select * from logininfo”)

>>> rs=cr.fetchall()
>>> print rs
[(‘40288a8542746fd90142746fdbb50001’, ‘cccccc’, 1, 1, none, none, none), (‘40288a85427474b601427474b8270001’, ‘eeee’, 1, 1, none, none, none), (‘40288a854273bce0014273bee6310002’, ‘dddddd’, 0, 0, none, none, none), (‘40288a854274532d014274532f600001’, ‘cccccc’, 1, 1, none, none, none), (‘40288a8542747c750142747c77ac0001’, ‘eeee’, 1, 1, none, none, none), (‘40288a8542744fb30142744fb5e90001’, ‘cccccc’, 1, 1, none, none, none)]
>>>

更多python安装oracle扩展及数据库连接方法相关文章请关注php中文网!

Posted in 未分类

发表评论