oracle windows客戶端下載地址:http://www.oracle.com/technetwork/topics/winx64soft-089540.htmlhtml
oracle鏈接工具下載地址:https://www.allroundautomations.com/bodyplsqldevreg.htmlpython
pip install cx_Oracle
import cx_Oracle as cx
conn = cx.connect('sys/password@localhost/orcl') cursor = conn.cursor () cursor.execute ("select * from dual") row = cursor.fetchone () print (row) cursor.close () conn.close ()
下載地址:http://sourceforge.net/projects/cx-oracle/files/?source=navbarlinux
我下載的是的cx_Oracle-5.1.2.tar.gzsql
使用cx_Oracle必需要安裝Oracle_client端,或者你已經安裝了Oracle數據庫。數據庫
下載地址:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.htmlwindows
以鏈接Oracle11爲例須要下載如下rpm包:bash
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm服務器
oracle-instantclient11.2-jdbc-11.2.0.4.0-1.x86_64.rpmoracle
oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm工具
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-odbc-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-tools-11.2.0.4.0-1.x86_64.rpm
軟件包都下載完後,咱們開始來安裝。
# rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm oracle-instantclient11.2-jdbc-11.2.0.4.0-1.x86_64.rpm oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm oracle-instantclient11.2-odbc-11.2.0.4.0-1.x86_64.rpm oracle-instantclient11.2-tools-11.2.0.4.0-1.x86_64.rpm # echo /usr/lib/oracle/11.2/client64/lib/ >> /etc/ld.so.conf # ldconfig
若是不進行ldconfig配置,在運行cx_Oracle時會報如下錯誤:
ibclntsh.so.11.1: cannot open shared object file: No such file or directory
在這裏須要說明下,你使用哪一個賬戶裝cx_Oracle就須要配置哪一個賬戶的環境變量,如下已root賬戶爲例;
若是不配置環境變量、或環境變量配置不正確,在安裝cx_Oracle時,會報各類錯誤,好比說:
oci.h: No such file or directory #vi ~/.bashrc export TNS_ADMIN="/usr/lib/oracle" export ORACLE_HOME="/usr/lib/oracle/11.2/client64" export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${ORACLE_HOME}/lib" export PATH="${PATH}:${ORACLE_HOME}" #source ~/.bashrc
#tar -zxvf cx_Oracle-5.1.2.tar.gz #cd cx_Oracle-5.1.2 #python setup.py install
[root@HAProxy01 Scripts]# python Python 3.3.0 (default, Sep 14 2017, 14:53:20) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cx_Oracle >>>
#!/usr/bin/python # -*- coding: UTF-8 -*- import cx_Oracle as db import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' def queryOracle(sql): username = "US_ORDER" #用戶名 passwd = "******" #用戶密碼 host = "192.168.1.203" #服務器IP port = "1521" #oracle端口 sid = "ORCL" #oracle的 dsn = db.makedsn(host, port, sid) con = db.connect(username, passwd, dsn) cur = con.cursor() cur.execute(sql) result = cur.fetchall() cur.close() con.close() return result if __name__=="__main__": sql = "select sysdate from dual" #須要執行的SQL result = queryOracle(sql) print(result)
執行結果:
[root@HAProxy01 Scripts]# ./check_oracle_tables.py [(datetime.datetime(2017, 9, 15, 12, 1, 51),)]