cx_Oracle & 远程连接到 Oracle 数据库
如何通过 IP 地址连接到远程服务器,就像 TOAD、SqlDeveloper 能够仅使用 IP 地址、用户名、SID 和密码连接到数据库一样?
每当我尝试指定 IP 地址时,它似乎都是在本地获取的。
换句话说,cx_Oracle.connect() 的字符串应该如何格式化为非本地数据库?
之前有一篇文章列出了通过 cx_Oracle 模块连接到 Oracle 的答案,代码如下:
#!/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()
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?
Whenever I try to specify and IP address, it seems to be taking it locally.
In other words, how should the string for cx_Oracle.connect() be formatted to a non local database?
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我喜欢这样做:
我喜欢这种方法的主要原因之一是我通常在某个地方有一个 TNSNAMES.ORA 文件,我可以检查 dsn_tns 对象是否会正确执行通过这样做:
并将输出与我的 TNSNAMES.ORA 进行比较
I like to do it this way:
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:and comparing the output to my TNSNAMES.ORA
您可以在连接字符串中指定服务器,例如:
You can specify the server in the connection string, e.g.:
您可以创建一个 dsn 并通过 service_name 连接,而不是指定 SID 就像:
使用服务名称而不是特定实例标识符 (SID) 的好处是它也可以在 RAC 环境中工作(使用 SID 则不行)。 此参数自 cx_Oracle 版本 5.1.1(2011 年 8 月 28 日)起可用
Instead of specifying the SID, you can create a dsn and connect via service_name like:
The benefit of using the service name instead of the specific instance identifier (SID), is that it will work in a RAC environment as well (using a SID won't). This parameter is available as of cx_Oracle version 5.1.1 (Aug 28, 2011)