Python 不会连接到数据库
Python will not connect to database
我正在尝试通过 python 连接到数据库。尝试 运行 python:
中的代码时,我不断收到此错误
DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
我知道 tns 设置很好,因为我可以使用同一台计算机通过 sql 开发人员连接到数据库。 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_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)
我正在尝试通过 python 连接到数据库。尝试 运行 python:
中的代码时,我不断收到此错误DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
我知道 tns 设置很好,因为我可以使用同一台计算机通过 sql 开发人员连接到数据库。 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_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)