日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

cx oracle dpi 1002,cx_Oracle连接数据库总结

發布時間:2024/9/27 168 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cx oracle dpi 1002,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文件

4.On Windows, check that the correct Windows Redistributables have been installed.

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

On Linux, check the LD_LIBRARY_PATH environment variable contains the Oracle Client library directory.

On macOS, make sure Oracle Instant Client is in ~/lib or /usr/local/lib and that you are not using the bundled Python (use Homebrew or Python.org instead).

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

1 importcx_Oracle2

3 connection = cx_Oracle.Connection("[email?protected]:Port/SERVICE_NAME")4 cursor =connection.cursor()5

6 try:7   cursor.execute("select 1 / 0 from dual")8 exceptcx_Oracle.DatabaseError as exc:9   error, =exc.args10 print("Oracle-Error-Code:", error.code)11 print("Oracle-Error-Message:", error.message)

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

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

下一行增加

print(cursor.description)

便可以看見查找到的數據庫中的內容

總結

以上是生活随笔為你收集整理的cx oracle dpi 1002,cx_Oracle连接数据库总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。