python cx_Oracle模塊的安裝和使用


 

 

$wget http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip

3.安裝配置

復制代碼
$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cd instantclient_10_2
$cp * /usr/lib   #直接放到動態庫搜索路徑中,不需要額外的環境配置

或
$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cp -rf instantclient_10_2 /opt/
$vi /etc/profile
      export ORACLE_HOME=/opt/instantclient_10_2
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

$source /etc/profile
復制代碼

4.配置tnsnames.ora(可不用配置tns)

注意tnsnames.ora其實並不存在,是要自己創建的(這個也很惡心,我一開始以為還要安裝什么東東。。),我沒有使用這種方式,有興趣的可以google一下。

 

5.下載安裝cx_Oracle python模塊

$wget http://downloads.sourceforge.net/project/cx-oracle/5.1.2/cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
$rpm -ivh cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
$ls /usr/lib/python2.6/site-packages/cx_Oracle.so #有這個文件表示安裝成功,根據python的位置,也可能在其他地方,自己找一下吧

6.驗證及問題解決

$python
>>import cx_Oracle

若報錯:import cx_Oracle gave ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory

表示沒有找到instant client的動態庫,check一下環境變量是否配置,是否生效,版本是否正確。

若報錯:ImportError: ./cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode

Google的信息:There is nothing wrong with Debian. Python supports two incompatible  modes of operation for Unicode, UCS2 (the default), and UCS4. Debian  uses the default, Redhat uses UCS4. You need to recompile the extension  for UCS-2 mode (i.e. using a Debian installation); this would fix the undefined symbol: PyUnicodeUCS4_Decode

所以重新編譯python

$./configure --prefix=/usr/local/python2.6.5 --enable-shared -enable-unicode=ucs4
$make;make install

再次驗證,終於正常import了。

 

使用:

1.基本連接–使用Oracle tns alias

復制代碼
connection =cx_Oracle.connect("tp/tp@ocn_test")
#查看tns alias命令
cmd>tnsping ocn_test
TNS Ping Utility forLinux: Version 9.2.0.8.0-Production on 27-SEP-201110:47:48
Copyright (c) 1997, 2006, Oracle Corporation.  Allrights reserved.
Used parameter files:
/opt/……/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL =TCP)(HOST =10.20.36.19)(PORT =1520))) (CONNECT_DATA =(SID =ocntest)))
OK (10msec)
復制代碼

2.用戶輸入密碼連接

pwd =getpass.getpass()
connection =cx_Oracle.connect("tp",pwd,"ocn_test")

3.用戶直接在Python命令中輸入連接賬號信息,格式如python script.py tp/tp@ocn_test

connection =cx_Oracle.connect(sys.argv[1])

4.使用Easy Connect語法,通過Drive連接數據庫

connection =cx_Oracle.connect('tp','tp','10.20.36.19:1521/ocntest')
#or
connection =cx_Oracle.connect('tp/tp@10.20.36.19:1521/ocntest')

5.先使用DSN構成TNSNAME

tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ')
connection =cx_Oracle.connect('tp','tp',tns_name)

6.登陸as SYSDBA

connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSDBA)
#or as SYSOPER
connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSOPER)

 

在Linux服務器執行Oracle操作時報了一個錯誤:

TNS:listener does not currently know of service requested in connect descriptor

解決方式:

問題分析見http://ora-12514.ora-code.com/,一番折騰,最后使用第5種連接方式,瞬間解決此問題。

 

 

import cx_Oracle 
conn = cx_Oracle.connect('用戶名/密碼@tnsname里面配置數據庫名') 
cursor = conn.cursor() 
cursor.execute("select to_char(sysdate,'yyyymmdd') from dual") 
rs = cursor.fetchone()  
print('result = %s'%rs)
result = 1
cursor.close()
conn.close() 

 


免責聲明!

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



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