python連接oracle數據庫


 1. 下載並安裝oracle-instantclient11.2.x

linux

[root]# rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
Preparing...                ########################################### [100%]
   1:oracle-instantclient11.########################################### [100%]

# rpm -ivh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm 

# rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.3.0-1.x86_64.rpm 

安裝完成后,ORACLE Instant Client 相關的頭文件在/usr/include/oracle/11.2/client64/下;庫文件在/usr/lib/oracle/11.2/client64/下(lib 和 bin兩個目錄.)
 
指定TNS文件目錄
一般情況將TNS文件(tnsnames.ora)保存到client64/network/admin下,該目錄不存在,先創建目錄
#mkdir -p /usr/lib/oracle/11.2/client64/network/admin
 將已有的TNS文件(tnsnames.ora)復制到該目錄下

頭文件的轉移
相關的頭文件(.h文件)在/usr/include/oracle/11.2/client64/目錄下,在client64下創建rdbms/public,把頭文件復制到該目錄下
#mkdir -p /usr/lib/oracle/11.2/client64/rdbms/public
[root]# cd /usr/include/oracle/11.2/client64
[root]# cp * /usr/lib/oracle/11.2/client64/rdbms/public/
[root]# cd /usr/lib/oracle/11.2/client64/rdbms/public/
[root]# ls
ldap.h     nzt.h     occiCommon.h   occiData.h  occiObjects.h  ocidef.h  ocidfn.h  oci.h      ocixmldb.h  odci.h        orid.h      orl.h  ort.h……



設置環境變量
#vi ~/.bash_profile     ( /etc/profile 修改全局變量)
加入:
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export TNS_ADMIN=$ORACLE_HOME/network/admin
export NLS_LANG="SimplifiedChinese_china".AL32UTF8
#export NLS_LANG='simplifiedchinese_china'.ZHS16GBK
export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib/oracle/11.2/client64/lib
PATH=$JAVA_HOME/bin:$PATH:$ORACLE_HOME/bin
export PATH

 

windows

下載地址http://www.oracle.com/technetwork/topics/winx64soft-089540.html

假設安裝在C盤

 

在C:\instantclient_11_2設置

tnsnames.ora

testDB =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 111.111.111.111)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = ORCL)
    )
  )

 

 2. 下載安裝cx_Oracle模塊

linux

tar -xzvf cx_Oracle-6.1.tar.gz 

cx_Oracle-6.1> python setup.py build cx_Oracle-6.1> python setup.py install
#基本連接–使用Oracle tns alias py
> python Python 3.6.0 (default, Nov 16 2017, 13:18:00) [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cx_Oracle >>> connection =cx_Oracle.connect('t1/t1@dba') >>> connection <cx_Oracle.Connection to t1@dba> >>> print(connection.version) 11.2.0.4.0 >>>

 

window

安裝cx_Oracle-6.3.1-cp36-cp36m-win_amd64.whl 

D:\>pip install wheel
Requirement already satisfied: wheel in c:\program files\python36\lib\site-packa
ges
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' comm
and.

D:\>pip install cx_Oracle-6.3.1-cp36-cp36m-win_amd64.whl
Processing d:\cx_oracle-6.3.1-cp36-cp36m-win_amd64.whl
Installing collected packages: cx-Oracle
  Found existing installation: cx-Oracle 6.0.3
    Uninstalling cx-Oracle-6.0.3:
      Successfully uninstalled cx-Oracle-6.0.3
Successfully installed cx-Oracle-6.3.1

導入

D:\>python
Python 3.6.1 (v3.6.1:69c0db5) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import cx_Oracle
>>> connection =cx_Oracle.connect('test/test@testDB')
>>> connection
<cx_Oracle.Connection to test@testDB>
>>> print(connection.version)
11.2.0.4.0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM