操作系统版本:rhel6.5
python版本:2.7
数据库和客户端版本:11.2.0.4
1、安装cx_Oracle模块
从https://pypi.org/project/cx-Oracle/#files 下载所需的包cx_Oracle-7.3.0.tar.gz 解压 tar -zxvf cx_Oracle-7.3.0.tar.gz 安装 cd cx_Oracle-7.3.0 python2 setup.py install
2、链接数据库的脚本
[root@ty14165 ty]# cat conn.py # -*- coding:utf-8 -*- import cx_Oracle as cx db = cx.connect('ty/tytest@hostname:1521/db') cur = db.cursor() sql = 'select ip from tytab' cur.execute(sql) rs = cur.fetchall() print rs cur.close() db.close()
3、执行结果
[root@ty14165 ty]# python2 conn.py [('192.168.14.165',), ('192.168.14.166',)]
执行过程如果有下面报错,可能是Oracle客户端没安装或者配置有问题
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory
安装和配置Oracle客户端
从https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html下载Oracle客户端安装包 安装 rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm 新增libclntsh.so包的软连接 cd /usr/lib/oracle/11.2/client64/lib ln -s libclntsh.so.11.1 libclntsh.so
来源:https://www.cnblogs.com/tonnytangy/p/12589073.html