cx_Oracle&远程连接到Oracle DB

cx_Oracle&远程连接到Oracle DB

问题描述:

如何通过IP地址连接到远程服务器,以使TOAD,SqlDeveloper能够仅使用IP地址,用户名,SID和密码连接到数据库?

How do you connect to a remote server via IP address in the manner that TOAD, SqlDeveloper, are able to connect to databases with just the ip address, username, SID and password?

每当我尝试指定IP地址时,它似乎都在本地使用。

Whenever I try to specify and IP address, it seems to be taking it locally.

换句话说,cx_Oracle.connect( )格式化为非本地数据库?

In other words, how should the string for cx_Oracle.connect() be formatted to a non local database?

以前有一篇文章被列为答案,通过cx_Oracle模块通过以下代码连接到Oracle:

There was a previous post which listed as an answer connecting to Oracle via cx_Oracle module with the following code:

#!/usr/bin/python

import cx_Oracle
connstr='scott/tiger'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

curs.execute('select * from emp')
print curs.description
for row in curs:
    print row
conn.close()


我喜欢这样:

ip = '192.168.0.1'
port = 1521
SID = 'YOURSIDHERE'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)

db = cx_Oracle.connect('username', 'password', dsn_tns)

我喜欢此方法的主要原因之一是,我通常在某个地方放有TNSNAMES.ORA文件,并且可以检查 dsn_tns 对象将做正确的事情:

One of the main reasons I like this method is that I usually have a TNSNAMES.ORA file lying around someplace, and I can check that the dsn_tns object will do the right thing by doing:

print dsn_tns

并将输出与我的TNSNAMES.ORA进行比较

and comparing the output to my TNSNAMES.ORA