Python-SQLAlchemy-Oracle连接无法连接到SID [英] Python - sqlalchemy - oracle connection fails to connect to SID
问题描述
我正在尝试使用 sqlalchemy
连接到Oracle数据库。考虑到 sqlalchemy文档。
I am trying to use sqlalchemy
to connect to an oracle DB. I was expecting the following to work given that it appears the exact syntax is shown in the sqlalchemy documentation.
oracle_db = sqlalchemy.create_engine('oracle://user:pass@server:1521/dev')
但这会导致错误:
dsn = self.dbapi.makedsn(url.host,port,** makedsn_kwargs)
TypeError:makedsn()不带关键字参数
dsn = self.dbapi.makedsn(url.host, port, **makedsn_kwargs) TypeError: makedsn() takes no keyword arguments
以下调用最初在没有服务名称的情况下起作用
The following call initially works without the service name
oracle_db = sqlalchemy.create_engine('oracle://user:pass@server:1521')
但是尝试连接失败,并显示错误消息,提示未提供 SERVICE_NAME
。
But when trying to connect it fails with an error complaining that the SERVICE_NAME
was not provided.
ORA-12504:在CONNECT_DATA中没有为TNS:listener提供SERVICE_NAME
ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA
奇怪的是,它直接与 cx_Oracle
一起使用:
Oddly this works with cx_Oracle
directly:
con = cx_Oracle.connect('user/pass@server:1521/dev')
我应该如何连接到特定服务?
How am I supposed to connect to the specific service?
尝试
我尝试从
I have tried to use cx_Oracle.makedsn()
explicitly from this question with no luck as well.
尝试使用?
连接字符串中的选项
Trying to use ?
options in the connection string
oracle_db = sqlalchemy.create_engine('oracle://user:pass@server:1521/?sid=dev')
最初可以工作,但是当我尝试 oracle_db.connect时()
我收到与上面显示的相同的 ORA-12504
错误。
works initially but when I try oracle_db.connect()
I get the same ORA-12504
error shown above.
推荐答案
基于 Sqlalchemy文档,您可能应该使用cx_oracle引擎。连接字符串为:
Based on the documentation at Sqlalchemy Documentation, you should probably use the cx_oracle engine. The connect string is:
oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
,带有service_name或sid选项,如下所示:
with an option of service_name or sid as follows:
oracle+cx_oracle://user:pass@host:1521/?service_name=hr
oracle+cx_oracle://user:pass@host:1521/?sid=hr
这篇关于Python-SQLAlchemy-Oracle连接无法连接到SID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!