Search code examples
python-2.xcx-oracleconfigparser

Using variables in cx_Oracle.connect method


I'm trying to read env.properties file to get the credentials to connect to Oracle DB using cx_Oracle. Variables have correct values but I guess issue is with the way I'm using connect method. Is there something wrong?

config = ConfigParser.RawConfigParser()
config.read('env.properties')

user = config.get('DatabaseSection', 'database.user')
pwd  =  config.get('DatabaseSection', 'database.password')
host = config.get('DatabaseSection', 'database.db_host')
port = config.get('DatabaseSection', 'database.db_port')
instance = config.get('DatabaseSection', 'database.db_instance')
conn = cx_Oracle.connect(user,pwd,host:port/instance)
cur = conn.cursor()

Solution

  • Managed with below piece of code -

    dsn_tns = cx_Oracle.makedsn(host, port, instance)
    conn = cx_Oracle.connect(user=user, password=pwd, dsn=dsn_tns)