cx_Oracle
的使用需要调用Oracle
环境,因此需要安装Oracle
客户端(推荐oracle instantclient)
官网地址:http://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.html
官网下载:http://www.oracle.com/technetwork/topics/winx64soft-089540.html
下载安装包:
instantclient-basic-windows.x64-11.2.0.4.0
instantclient-sdk-windows.x64-11.2.0.4.0
instantclient-sqlplus-windows.x64-11.2.0.4.0
a.解压在同一个目录下
b.配置环境变量 E:\software\instantclient_11_2
c.测试sqlplus
E:\software\instantclient_11_2>sqlplus.exe username/password@//host:port/sid
SQL> select * from database."tablename";
pip install cx_Oracle==5.3
报错:
error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
打开 http://aka.ms/vcpython27 下载
报错:
InterfaceError: Unable to acquire Oracle environmen
将oracle目录下的几个dll文件copy到python site-package目录下
>>> import cx_Oracle
>>> conn = cx_Oracle.connect(username, password, host:port/sid)
>>> result = conn.cursor().execute("SELECT * FROM TABLE")
>>> for item in result:
... print item
>>> conn.close()
>>> exit()
官网下载:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
下载安装包:
(1)zip安装
instantclient-basic-linux.x64-11.2.0.4.0.zip
instantclient-sdk-linux.x64-11.2.0.4.0.zip
instantclient-sqlplus-linux.x64-11.2.0.4.0.zip
解压三个文件到同一个目录下
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip -d /software/
unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip -d /software/
unzip instantclient-sqlplus-linux.x64-11.2.0.4.0.zip -d /software/
/software/instantclient_11_2/network/admin要自己创建,包括tnsnames.ora文件
$ORACLE_HOME/network/admin/tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = XXXX)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
# vim /etc/profile #文件尾部添加
export ORACLE_HOME=/software/oracle/instantclient_12_2
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
export TNS_ADMIN=$ORACLE_HOME/network/admin # 安装了sqlplus 要设置监听
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK # 设置编码集
PATH = $ORACLE_HOME:...
# source /etc/profile
cd /software/instantclient_11_2/
ln -s libclntsh.so.11.1 libclntsh.so
pip install cx_Oracle==5.3
(venv) [root@localhost /]# python
>>> import cx_Oracle
>>> print cx_Oracle.version
5.3
>>> exit()