博客 2016-03-11

简介:

python 连接Oracle 过程总结(Linux)


下载Oracle客户端: 

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

需要注册


下面几个(py27代表python2.7版本):

cx_Oracle-5.1.2-11g-py27-1.x86_64.rpm (https://sourceforge.net/projects/cx-oracle/files/)

instantclient-basic-linux.x64-12.1.0.2.0.zip

instantclient-sdk-linux.x64-12.1.0.2.0.zip


安装过程:

1.安装包

$unzip instantclient-basic-linux.x64-12.1.0.1.0.zip
$sudo cp -r instantclient_12_1 /opt

unzip instantclient-sdk-linux.x64-12.1.0.2.0.zip
cp sdk instantclient_12_1

$vi /etc/profile
export ORACLE_HOME=/opt/instantclient_12_1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

$source /etc/profile


2.用PIP安装

pip install cx_Oracle


连接方法: 

import cx_Oracle
con = cx_Oracle.connect( "username", "passwd","(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.2.2.4)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = mips)))")
curs=con.cursor ()


sql='SELECT * FROM all_users'
rr=curs.execute (sql)
row=curs.fetch()
print row

 

Oracle 用例PL/SQL(windows): 

下载PL/SQL(需要注册)

安装Oralce 客户端


问题:


1.ImportError: libaio.so.1: cannot open shared object file: No such file or directory

原因:yum install libaio


2。ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory

原因:ln -s libclntsh.so.12.1 libclntsh.so.11.1


3.  /bin/ld: cannot find -lclntsh

原因:

cd  /apps/lib/instantclient_12_1

ln -s libclntsh.so.12.1 libclntsh.so


参考资料: