wolf7687
wolf7687

Reputation: 155

Python will not connect to database

I'm trying to connect to a database via python. I keep getting this error when trying to run the code in python:

DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified

I know the tns settings are good because I can connect to the database via sql developer using the same computer. What's wrong with Python.

host = '205.218.7.153'
port = '1521'
sid= 'pdevl3'
username = 'uname'
password = 'pwd'

connect_str = username + '/' + password + '@' + host + ':' + port + '/' + sid 
orcl = cx_Oracle.connect(connect_str)
curs = orcl.cursor()
curs.execute(query2)
rows = curs.fetchall()
curs.close()

Upvotes: 1

Views: 563

Answers (1)

M Z
M Z

Reputation: 4799

Instead of building the string yourself, try using cx_Oracle to help build it for you:

import cx_Oracle

host = '205.218.7.153'
port = '1521'
sid= 'pdevl3'
username = r'uname' # make sure to use an r string if you have any special characters
password = r'pwd'

dsn_tns = cx_Oracle.makedsn(host, port, service_name=sid)
orcl = cx_Oracle.connect(user=username, password=password, dsn=dsn_tns)

Upvotes: 1

Related Questions