如何使用带有服务名称和登录名的 cx_Oracle 连接到 Oracle 数据库?

How to connect to an Oracle Database using cx_Oracle with service name and login?

我有登录名、端口号、主机名和服务名。我将如何使用 cx_Oracle.connect()?

是这样的吗?

import cx_Oracle
con = cx_Oracle.connect('username/password@host_name/service_name')
print con.version
con.close()

这对我有用,你通常需要一个端口号

import cx_Oracle as orc 
user= 'username'
pwd = 'password'
host = 'url.or.path.to.db' #could look like path or url depending on where it's hosted
service_name = 'servicename'
portno = 1234 #note this is dummy port no

con = orc.connect(user, pwd, '{}:{}/{}'.format(host,portno,service_name))

在我的例子中,我使用了:

con = cx_Oracle.connect('username/password@'service_name')

无需使用主机名 oracle 可以隐式连接主机名,您只需给服务名称即可。