python使用cx_Oracle連接oracle


 

1.使用pip命令安裝cx_Oracle

$ pip install cx_Oracle

2.安裝oracle客戶端,並添加到path

下載路徑:

http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html

根據對應的系統類型和版本以及oracle版本進行下載,比如64位python的windows平台,oracle版本為11g,

下載instantclient-basic-windows.x64-11.2.0.4.0.zip

http://www.oracle.com/technetwork/topics/winx64soft-089540.html

解壓至安裝路徑,然后將該路徑添加至環境變量path后,比如解壓在C:\Oracle路徑下,那么在path環境變量后面加上C:\Oracle\instantclient_11_2;即可

3.使用示例

import cx_Oracle conn = cx_Oracle.connect('user/passwd@host/instance') curs = conn.cursor() sql = 'select * from product_component_version' curs.execute(sql) for result in curs: print(result) curs.close() conn.close()



cx_Oracle連接數據庫總結

 

python中連接oracle數據庫使用第三方庫文件cx_Oracle時遇到了各種問題,網上查找資料調試了幾天才弄好,下面是不斷調試后總結的一些經驗。
1.oracle客戶端(Oracle Instant Client)版本需要和操作系統版本位數相同,同時cx_Oracle官方文檔(http://cx-oracle.readthedocs.io/en/latest/installation.html)上有這樣一段話

Version 12.2 client libraries can connect to Oracle Database 11.2 or greater. Version 12.1 client libraries can connect to Oracle Database 10.2 or greater. Version 11.2 client libraries can connect to Oracle Database 9.2 or greater.
根據安裝的Oracle數據庫版本選擇Oracle客戶端(下載地址 http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html),我安裝的oracle數據庫是11g版本,這里的客戶端庫在下載客戶端Oracle Instant Client時就包含在內

下載好oracle客戶端后,在客戶端目錄下新建一/network/admin目錄,並在該目錄下新建tnsnames.ora文件,增加自己的數據庫別名配置。
示例如下:

復制代碼
1 MyDB=
2 (DESCRIPTION =
3 (ADDRESS = (PROTOCOL = TCP)(HOST= IP)(PORT = 1521))
4 (CONNECT_DATA =
5 (SERVER = DEDICATED)
6 (SERVICE_NAME = )
7 )
8 )
復制代碼

注意格式要排列好
MyDB為Database名,Host為IP地址, SERVICE_NAME為數據庫服務器的實例名。

2.安裝的python版本位數也需與操作系統版本位數相同

3.安裝的第三方庫cx_Oracle需要版本位數和操作系統相同同時,與Oracle數據庫對應的版本也應相同,因我安裝的數據庫是11g,所以下載的是cx_Oracle-5.3-11g.win-amd64-py3.5-2 若安裝的數據庫是12c則應下載相應版本cx_Oracle(地址 https://pypi.python.org/pypi/cx_Oracle/5.3)

同時可能出現的其他問題在cx_Oracle官方文檔中也寫出了
1. DPI-1047: Oracle Client library cannot be loaded
Check that Python, cx_Oracle and your Oracle Client libraries are all 64-bit or all 32-bit. The DPI-1047 message will tell you whether the 64-bit or 32-bit Oracle Client is needed for your Python.
檢查python,cx_Oracle和Oracle Instant Client版本是否一致,DPI-1047 message會告訴你安裝的客戶端版本是否正確。

2.On Windows, restart your command prompt and use set PATH to check the environment variable has the correct Oracle Client listed before any other Oracle directories.
記得配置Oracle客戶端的環境變量,例如我的配置是 PATH: E:\oracle\instantclient_12_2;

3.On Windows, use the DIR command on the directory set in PATH. Verify that OCI.DLL exists there.
檢查oracle客戶端(instantclient)目錄下存在oci.dll文件

oracle客戶端libraries需要正確的Visual Studio版本,具體可見(https://oracle.github.io/odpi/doc/installation.html#windows)中windows目錄下

最后一切就緒,程序未出錯,但並無輸出時感覺內心有些崩潰

剛剛開始連接時,我機器上的oracle是32位客戶端連64位服務器端,客戶端位於 D:\app\username\product\11.2.0\client_1

嘗試連接時,會報錯如下:

cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "d:\app\sabre\product\11.2.0\client_1\bin\oci.dll is not the correct architecture". See https://oracle.github.io/odpi/doc/installation.html#windows for help

按照提示中的鏈接,找到oracle的64位客戶端文件package,下載,放到D:\instantclient_11_2,然后把這個路徑添加到系統path中,再次嘗試連接,就可以了。

復制代碼
 1 import cx_Oracle
 2 
 3 connection = cx_Oracle.Connection("Username/密碼@Host:Port/SERVICE_NAME")
 4 cursor = connection.cursor()
 5 
 6 try:
 7   cursor.execute("select 1 / 0 from dual")
 8 except cx_Oracle.DatabaseError as exc:
 9   error, = exc.args
10 print("Oracle-Error-Code:", error.code)
11 print("Oracle-Error-Message:", error.message)
復制代碼

最后查看意識到是執行了sql語句,但並未進行輸出

cursor.execute("select 1 / 0 from dual")

 


下一行增加
 print(cursor.description) 


免責聲明!

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



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