cx_Oracle 我使用的主机名不正确吗?

cx_Oracle Am I using the host name incorrectly?

我使用 Toad 连接到我的 Oracle 数据库并且工作正常但是我试图使用 cx_Oracle 连接到同一个数据库并且我的程序卡在 .connect() 方法。

我认为问题出在 host 名称上,但我无法确定是否存在任何错误,只是没有通过 connect() 调用。

import cx_Oracle


CONN_INFO = {'host': '\\SERVERNAMEUSEDINTOAD\',
             'port': 1111,
             'user': 'USER123',
             'psw': 'password',
             'service': 'dbname.somesite.com'}
print("test1")
CONN_STR = '{user}/{psw}@{host}:{port}/{service}'.format(**CONN_INFO)
print("test2")
con = cx_Oracle.connect(CONN_STR)
print("test3")
print(con.fetchmany([1]))

控制台:

test1
test2

我做到了 test2 但没有做到 test3。我是否正确使用了主机名?主机是网络上的内部服务器。

TOAD 应用程序仅显示 SERVERNAMEUSEDINTOAD 所以我也尝试了:

'host': 'SERVERNAMEUSEDINTOAD'

但这导致了以下错误:

test1
test2
Traceback (most recent call last):
  File "C:/Users/name/PycharmProjects/WorkFlow/test.py", line 12, in <module>
    con = cx_Oracle.connect(CONN_STR)
cx_Oracle.DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified

经过一些挖掘,我发现了这个 post:

cx_Oracle doesn't connect when using SID instead of service name on connection string.

以为是关于与SID连接有一个post建议使用makedsn()然后使用这个连接。

事实证明,通过 service_name.

连接也解决了我的问题

我post在这里提出我的解决方案,所以任何发现这个 post 的人都可以选择解决这个问题。

import cx_Oracle

or_dns = cx_Oracle.makedsn('SERVERNAMEUSEDINTOAD', 1111, service_name='dbname.somesite.com')

con = cx_Oracle.connect(user="USER123", password="password", dsn=or_dns)
print(con)

控制台:

<cx_Oracle.Connection to USER123@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SERVERNAMEUSEDINTOAD)(PORT=1111))(CONNECT_DATA=(SERVICE_NAME=dbname.somesite.com)))>

我必须做两件事才能让您的示例在我的笔记本电脑上运行。

我必须更新我的 sqlnet.ora 才能拥有这条线:

names.directory_path = (TNSNAMES,EZCONNECT)

而不是

names.directory_path = (TNSNAMES)

然后我不得不使用主机的 IP 地址而不是主机名。

我认为这不是 cx_oracle 中的错误,因为 sqlplus 也无法使用相同的语法。但是,它与我的 sqlnet.ora.

中的 IP 地址和 EZCONNECT 一起工作

鲍比