简体   繁体   English

sqlalchemy无法连接,但cx_oracle成功

[英]sqlalchemy fails to connect but cx_oracle succeeds

I'm attempting to connect to an oracle server in Python. 我正在尝试使用Python连接到Oracle服务器。 I have this working in cx_Oracle, but when I try to connect using sqlalchemy it fails. 我可以在cx_Oracle中使用它,但是当我尝试使用sqlalchemy连接时会失败。 The cx_Oracle code: cx_Oracle代码:

import cx_Oracle
import pandas as pd

cx_connection = cx_Oracle.connect(user+'/' + pw + '@' + host + ':' + port + '/' + db)
df = pd.read_sql(my_query, cx_connection)

Executes and returns data from the database based on the query as expected. 根据查询执行并从数据库返回数据。 If I try the same connection with sqlalchemy: 如果我尝试使用sqlalchemy进行相同的连接:

import sqlalchemy

engine = sqlalchemy.create_engine('oracle+cx_oracle://' + user + ':' + pw + '@' + host + ':' + port + '/' + db)
sqlalchemy_connection = engine.connect()

I get an error on the last line: 我在最后一行收到错误:

DatabaseError: (cx_Oracle.DatabaseError) ORA-12505: TNS:listener does not currently know of SID given in connect descriptor (Background on this error at: http://sqlalche.me/e/4xp6 ) DatabaseError:(cx_Oracle.DatabaseError)ORA-12505:TNS:listener当前不知道连接描述符中给出的SID(有关此错误的背景,位于: http : //sqlalche.me/e/4xp6

Any idea what the problem is? 知道是什么问题吗? Isn't sqlalchemy just using cx_Oracle? sqlalchemy不只是使用cx_Oracle吗? Is there any workaround to just give the cx_Oracle connection to sqlalchemy? 有什么解决方法可以将cx_Oracle连接赋予sqlalchemy吗?

Per the doc , the format of your Oracle connection string with SQLAlchemy should be oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...] . 根据文档 ,使用SQLAlchemy的Oracle连接字符串的格式应为oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...] But based on your former connection string, the db value might actually be the TNS service name, so in this case, you want to use 但是根据您以前的连接字符串,db值实际上可能是TNS服务名称,因此在这种情况下,您想使用

engine = sqlalchemy.create_engine('oracle+cx_oracle://' + user + ':' + pw + '@' + host + ':' + port + '/?service_name=' + db)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM