简体   繁体   中英

Using multiple hosts on cx_oracle

I'm using the cx_oracle pip package to connect to a oracle DB at work.

dsn_tns = cx_Oracle.makedsn('1.2.3.4', '1521', service_name='TRADES')
conn = cx_Oracle.connect(user='analytics_user', password='welcome', dsn=dsn_tns)
c = conn.cursor()
c.execute('SELECT * FROM all_tables')

However the oracle db I connect to has 2 host IP addresses.

The original connection string I was given was like this;

SERVICE  =
   (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.4)(Port=1521))
     (ADDRESS = (PROTOCOL = TCP)(HOST = 1.222.33.44)(Port=1521))
     (CONNECT_DATA =
       (SERVICE_NAME = TRADES)
     )
   )

Is there anyway to use both on cx_oracle? or is there any other package I can use that will allow that? SQL_Alchemy?

you can use the DSN string as is, without the SERVICE = :

DSN = """
   (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.4)(Port=1521))
     (ADDRESS = (PROTOCOL = TCP)(HOST = 1.222.33.44)(Port=1521))
     (CONNECT_DATA =
       (SERVICE_NAME = TRADES)
     )
   )
"""

with cx_Oracle.connect(user='user', password='pass', dsn=dsn_tns) as conn:
    c = conn.cursor()
    c.execute('SELECT * FROM all_tables')

thanks @Christopher-Jones here the details oracle-net-connect-descriptor-strings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM