cx_Oracle & Connecting to Oracle DB Remotely

后端 未结 6 1971
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 04:59

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 pass

6条回答
  •  天命终不由人
    2020-11-28 05:28

    Instead of specifying the SID, you can create a dsn and connect via service_name like:

    import cx_Oracle
    ip = '192.168.0.1'
    port = 1521
    service_name = 'my_service'
    dsn = cx_Oracle.makedsn(ip, port, service_name=service_name)
    
    db = cx_Oracle.connect('user', 'password', dsn)
    

    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)

提交回复
热议问题