我正在尝试通过python连接到数据库。尝试在python中运行代码时,我不断收到此错误:
DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
我知道tns设置很好,因为我可以使用同一台计算机通过sql developer连接到数据库。 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()
答案 0 :(得分:1)
与其使用自己构建的字符串,不如尝试使用cx_Oracle
来帮助您构建字符串:
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)