python_连接oracle报错ORA-12505

报错详情为:
ORA-12505,TNS:listener does not currently know of SID given in connect descriptor

报错的提示为不清楚这个SID

在这里插入图片描述

  • 错误可能2
    Python连接工具中使用的是 SID连接,但我们错误的使用了服务名SERVICE_NAME

关于sid和service_name:
sid(system identifiers): The SID is the same as the $ORACLE_SID environment variable in the sense that the SID is the system is for a unique instance on the server.唯一地指向一个实例

service_name: is used to register an instance with the listener. 使用监听来注册实例。 a SERVICE_NAME can point to more than one instance 可以指向多个实例

从oracle9i版本开始,引入了一个新的参数,即数据库服务名。参数名是SERVICE_NAME。

需要注意的是,我们一般在Python中使用cx_Oracle连接数据库

在这里插入图片描述
一般的cx_oracle连接方式中使用的是service_name,如果有需要也可以使用dsn方法指定用sid。

conn = cx_Oracle.connect("用户名","密码",'ip:端口/服务名',encoding='UTF-8')

另一类使用pandas直接操作oracle,需要使用sqlalchemy创建引擎create_engine,这里容易搞混SID和SERVICE_NAME

在创建的时候需要指定用的是service_name,不是sid,否则会把输入的服务名当做sid,从而报错ORA-12505

指定方法:

engine = create_engine("oracle+cx_oracle://scott:tiger@hostname:port/?service_name=myservice&encoding=UTF-8&nencoding=UTF-8")

我们这里是第一次没有指定,指定后报错就好了

cx_Oracle和sqlalchemy的用法详细参考官方文档:

https://docs.sqlalchemy.org/en/14/dialects/oracle.html

https://cx-oracle.readthedocs.io/en/latest/user_guide/connection_handling.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值