1

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
asked Aug 27, 2020 at 19:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Another suggestion, regardless of the method you elect, is to print the assembled TNS string to a job log to that you may observe what is being submitted.
Thanks. That's crazy; it worked as is for like 2 months now it stopped working. I don't get it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.