我正在尝试通过 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()
1 回答
慕仙森
TA贡献1827条经验 获得超8个赞
不要自己构建字符串,而是尝试使用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)
添加回答
举报
0/150
提交
取消