Python链接Oracle数据库

Python链接Oracle数据库常见的三个错误

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)
  1. 找不到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"

这里的错误意思是 不能加载64位的oracle 客户端库,也就是你oracle的客户端instantclient弄错了,换成64位即可

  1. connect的参数数据流有问题
  2. 编码报错

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5

如果遇到上面那些错误需要确保电脑上的python版本以及Oracle都是同样版本(同为32或者64)以及配置oci.dll 环境变量后重启IDE即可

posted @ 2022-04-02 23:15  淡定的人参果  阅读(171)  评论(0编辑  收藏  举报