我試了幾次嘗試連接Oracle DB,但仍無法連接。以下是我的代碼連接。
$ sqlplus64 uid/[email protected]:1521/WSVCcx_Oracle連接python3.5
- 我的環境現狀:Ubuntu的16.04/64位/ Python3.5
我希望您能分享與此問題相關的知識和經驗。謝謝。
import os
os.chdir("/usr/lib/oracle/12.2/client64/lib")
import cx_Oracle
# 1st attempt
ip = '192.168.0.5'
port = 1521
SID = 'WSVC'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
# dsn_tns = cx_Oracle.makedsn(ip, port, service_name=SID)
db = cx_Oracle.connect('uid', 'passwd', dsn_tns)
cursor = db.cursor()
-------------------------------------------------
# 2nd attempt
conn = "uid/[email protected](DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.5)(PORT=1521)))(CONNECT_DATA=(SID=WSVC)(SRVR=DEDICATED)))"
db = cx_Oracle.connect(conn)
cursor = db.cursor()
------------------------------------------------------
# ERROR Description
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
您使用的是什麼版本的cx_Oracle?你設置了哪些環境變量(LD_LIBRARY_PATH,ORACLE_HOME)。 os.chdir()不應該出現在任何情況下! –
我正在使用cx_Oracle:5.3。我還在〜/ .bashrc文件中設置了以下內容:-------- ORACLE_HOME =/usr/lib/oracle/12.2/client64 export LD_LIBRARY_PATH =/usr/lib/oracle/12.2/client64:$ LD_LIBRARY_PATH export PATH =/usr/lib/oracle/12.2/client64:$ PATH –