cx oracle dpi 1002,cx_Oracle连接数据库总结
python中連接oracle數(shù)據(jù)庫使用第三方庫文件cx_Oracle時(shí)遇到了各種問題,網(wǎng)上查找資料調(diào)試了幾天才弄好,下面是不斷調(diào)試后總結(jié)的一些經(jīng)驗(yàn)。
1.oracle客戶端(Oracle Instant Client)版本需要和操作系統(tǒng)版本位數(shù)相同,同時(shí)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.
根據(jù)安裝的Oracle數(shù)據(jù)庫版本選擇Oracle客戶端(下載地址 http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html),我安裝的oracle數(shù)據(jù)庫是11g版本,這里的客戶端庫在下載客戶端Oracle Instant Client時(shí)就包含在內(nèi)
下載好oracle客戶端后,在客戶端目錄下新建一/network/admin目錄,并在該目錄下新建tnsnames.ora文件,增加自己的數(shù)據(jù)庫別名配置。
示例如下:
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為數(shù)據(jù)庫服務(wù)器的實(shí)例名。
2.安裝的python版本位數(shù)也需與操作系統(tǒng)版本位數(shù)相同
3.安裝的第三方庫cx_Oracle需要版本位數(shù)和操作系統(tǒng)相同同時(shí),與Oracle數(shù)據(jù)庫對(duì)應(yīng)的版本也應(yīng)相同,因我安裝的數(shù)據(jù)庫是11g,所以下載的是cx_Oracle-5.3-11g.win-amd64-py3.5-2 若安裝的數(shù)據(jù)庫是12c則應(yīng)下載相應(yīng)版本cx_Oracle(地址 https://pypi.python.org/pypi/cx_Oracle/5.3)
同時(shí)可能出現(xiàn)的其他問題在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會(huì)告訴你安裝的客戶端版本是否正確。
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客戶端的環(huán)境變量,例如我的配置是 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).
最后一切就緒,程序未出錯(cuò),但并無輸出時(shí)感覺內(nèi)心有些崩潰
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)
最后查看意識(shí)到是執(zhí)行了sql語句,但并未進(jìn)行輸出
在
cursor.execute("select 1 / 0 from dual")
下一行增加
print(cursor.description)
便可以看見查找到的數(shù)據(jù)庫中的內(nèi)容
總結(jié)
以上是生活随笔為你收集整理的cx oracle dpi 1002,cx_Oracle连接数据库总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 用下标获取map值_javascr
- 下一篇: linux cmake编译源码,linu