Python鏈接Oracle數(shù)據(jù)庫(kù)
Python鏈接Oracle數(shù)據(jù)庫(kù)常見(jiàn)的三個(gè)錯(cuò)誤
import cx_Oracle
print("cx_Oracle.version:", cx_Oracle.version)
host = "數(shù)據(jù)庫(kù)ip"
port = "1521"
service_name = "pdb01"
# dsn = cx_Oracle.makedsn(host, port, sid)
# 通過(guò)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) # 返回連接的游標(biāo)對(duì)象
cursor.execute("select * from ac01 where aac003='whosyourdaddy")
result = cursor.fetchall()
print (result)
- 找不到64位client
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found".
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "C:\Program Files\python\oci.dll is not the correct architecture"
這里的錯(cuò)誤意思是 不能加載64位的oracle 客戶端庫(kù),也就是你oracle的客戶端instantclient弄錯(cuò)了,換成64位即可
- connect的參數(shù)數(shù)據(jù)流有問(wèn)題
- 編碼報(bào)錯(cuò)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5
如果遇到上面那些錯(cuò)誤需要確保電腦上的python版本以及Oracle都是同樣版本(同為32或者64)以及配置oci.dll 環(huán)境變量后重啟IDE即可
浙公網(wǎng)安備 33010602011771號(hào)