首页 >后端开发 >Python教程 >oracle的安装及数据库连接的方法详解

oracle的安装及数据库连接的方法详解

Y2J
Y2J原创
2017-05-08 16:47:142101浏览

这篇文章主要介绍了python安装oracle扩展及数据库连接方法,较为详细的分析了Python下载oracle扩展及Windows、Linux环境下的安装步骤、操作技巧及注意事项,需要的朋友可以参考下

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

下载:

cx_Oracle下载地址:cx-oracle.sourceforge.net/
instantclient-basic下载地址:www.oracle.com/technetwork/database/features/instant-client/index-097480.html

window环境:

python27 oracle10

需要软件:

cx_Oracle-5.1.2-10g.win32-py2.7.msi
instantclient-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.rpm
basic-10.2.0.4.0-linux-x86_64.zip

1. rpm -ivh cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
2. (此处参考www.jb51.net/article/106295.htm)

设置环境变量

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
<cx_Oracle.Connection to user@114.242.113.91:1521/orcl>
>>> cr=db.cursor()
>>> cr.execute("select * from LOGININFO")
<builtin.OracleCursor on <cx_Oracle.Connection to user@114.242.113.91:1521/orcl>>
>>> rs=cr.fetchall()
>>> print rs
[(&#39;40288a8542746fd90142746fdbb50001&#39;, &#39;cccccc&#39;, 1, 1, None, None, None), (&#39;40288a85427474b601427474b8270001&#39;, &#39;eeee&#39;, 1, 1, None, None, None), (&#39;40288a854273bce0014273bee6310002&#39;, &#39;dddddd&#39;, 0, 0, None, None, None), (&#39;40288a854274532d014274532f600001&#39;, &#39;cccccc&#39;, 1, 1, None, None, None), (&#39;40288a8542747c750142747c77ac0001&#39;, &#39;eeee&#39;, 1, 1, None, None, None), (&#39;40288a8542744fb30142744fb5e90001&#39;, &#39;cccccc&#39;, 1, 1, None, None, None)]
>>>

【相关推荐】

1. Python免费视频教程

2. Python面向对象视频教程

3. Python学习手册

以上是oracle的安装及数据库连接的方法详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn