为了账号安全,请及时绑定邮箱和手机立即绑定

Python无法连接数据库

Python无法连接数据库

呼啦一阵风 2023-06-27 17:35:53
我正在尝试通过 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条经验 获得超7个赞

不要自己构建字符串,而是尝试使用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)


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 137 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信