Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将不会连接到数据库_Python_Oracle_Cx Oracle - Fatal编程技术网

Python将不会连接到数据库

Python将不会连接到数据库,python,oracle,cx-oracle,Python,Oracle,Cx Oracle,我正在尝试通过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 =

我正在尝试通过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()

尝试使用
cx\u Oracle
来帮助您构建字符串,而不是自己构建字符串:

导入cx\u Oracle
主机='205.218.7.153'
端口='1521'
sid='pdevl3'
username=r'uname'#如果您有任何特殊字符,请确保使用r字符串
密码=r'pwd'
dsn\u tns=cx\u Oracle.makedsn(主机、端口、服务\u name=sid)
orcl=cx\U Oracle.connect(用户=用户名,密码=密码,dsn=dsn\U tns)

另一个建议是,无论您选择哪种方法,都要将组装好的TNS字符串打印到作业日志中,以便您观察提交的内容。谢谢。那太疯狂了;它正常工作了2个月,现在停止工作了。我不明白。