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()
Barbaros Özhan
65.9k11 gold badges36 silver badges64 bronze badges
1 Answer 1
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)
answered Aug 27, 2020 at 19:56
M Z
4,8392 gold badges15 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
lang-py