1.一般的連接方法
即通過cx_Oracle包來實現
安裝方法
pip install cx_Oracle
pyhton 連接代碼
import cx_Oracle
print("cx_Oracle.version:", cx_Oracle.version)
host = "數據庫ip"
port = "1521"
service_name = "pdb01"
# dsn = cx_Oracle.makedsn(host, port, sid)
# 通過servername的方式連接
dsn = cx_Oracle.makedsn(host,port, service_name=service_name)
print(dsn)
connection = cx_Oracle.connect("oracle用戶名", "oracle密碼", dsn)
cursor = cx_Oracle.Cursor(connection) # 返回連接的游標對象
cursor.execute("select * from ac01 where aac003='whosyourdaddy")
result = cursor.fetchall()
print (result)
通常,你要是順利的話,那么就可以讀取到數據了。但是,請看下面
問題記錄
oracle 客戶端問題
這里錯誤的原因各種各樣,只記錄成功的方法
即要保證(pyhton cx_Oracle oracleclient )3個都必須是64位的。
當然也可以都32位 但是我沒嘗試過
cx_Oracle.DatabaseError: DPI-1047
這里問題有兩個
1.cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found".
2.x_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "C:\Program Files\python\oci.dll is not the correct architecture"
這里的錯誤意思是 不能加載64位的oracle 客戶端庫,也就是你oracle的客戶端弄錯了。換成64位即可
並且要把客戶端的dll ,copy一份到python.exe對應的目錄
我這里用的是instantclient_11_2。那就把目錄下的所有dll copy 到python目錄
cx_Oracle.DatabaseError: DPI-1072: the Oracle Client library version is unsupported
但你完成上面的修改后,可能還會遇到這個 1072的問題。
這個問題的解決辦法就是更換 instantclient 的版本。 我之前用instantclient21的時候就報這個錯。
換成instantclient11之后 就成功了