如何使用oracledb将Python连接到Oracle

问题描述 投票:0回答:1

我正在尝试使用以下 Python 脚本连接到 Oracle:

import oracledb  
connection = oracledb.connect(user = 'user', 
                                  password = 'PW', 
                                  service_name = 'xxx') 

但是,我收到以下错误:

操作错误:DPY-6005:无法连接到数据库(CONNECTION_ID = KKNOzerEHqa1cFi8jN5yWw==)。 [WinError 10061]无法建立连接,因为目标机器主动拒绝

如果我不得不猜测我输入了错误的service_name。我知道 TNS 服务名称和数据库服务名称是什么,但我不确定这些是否是我需要输入的内容。如果这些是正确的,则不清楚我到底要在此处输入什么,例如

service_name = 'local/service name'
或仅输入服务名称,不包括 'local/'

最后,我通常使用 JDBC 自定义 url 连接来连接到我的工作数据库,例如“jdbc:oracle:thin:@ldap://ldaptns...”。有谁知道是否可以在

oracledb.connect()
中使用它?

谢谢

python oracle oracle-sqldeveloper python-oracledb
1个回答
0
投票

您需要指定主机,正如约翰·戈登(John Gordon)指出的那样。像这样:

import oracledb

connection = oracledb.connect(
    user="user",
    password="PW",
    host="server_host",
    port=1521,       # this is optional
    service_name="xxx"
)

# or you can use this (Easy Connect string)

connection = oracledb.connect(
    user="user",
    password="PW",
    dsn="server_host:server_port/xxx"
)
© www.soinside.com 2019 - 2024. All rights reserved.