sqlalchemy无法连接,但是cx_oracle成功 [英] sqlalchemy fails to connect but cx_oracle succeeds

查看:638
本文介绍了sqlalchemy无法连接,但是cx_oracle成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python连接到oracle服务器。我在cx_Oracle中有此功能,但是当我尝试使用sqlalchemy连接时会失败。 cx_Oracle代码:

I'm attempting to connect to an oracle server in Python. I have this working in cx_Oracle, but when I try to connect using sqlalchemy it fails. The cx_Oracle code:

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)

根据查询执行并从数据库返回数据。如果我尝试使用sqlalchemy进行相同的连接:

Executes and returns data from the database based on the query as expected. If I try the same connection with sqlalchemy:

import sqlalchemy

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

最后一行出现错误:


DatabaseError:(cx_Oracle.DatabaseError)ORA-12505:TNS:listener确实
当前不知道连接描述符中给出的SID(在
的背景上,此错误位于: http://sqlalche.me/e/4xp6

有什么问题吗? sqlalchemy不只是使用cx_Oracle吗?是否有任何变通办法只是将cx_Oracle连接赋予sqlalchemy?

Any idea what the problem is? Isn't sqlalchemy just using cx_Oracle? Is there any workaround to just give the cx_Oracle connection to sqlalchemy?

推荐答案

每个 doc ,带有SQLAlchemy的Oracle连接字符串的格式应为 oracle + cx_oracle:// user:pass @ host:port / dbname [?key = value& key = value ...] 。但是根据您以前的连接字符串,db值实际上可能是TNS服务名称,因此在这种情况下,您想使用

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...]. 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

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

这篇关于sqlalchemy无法连接,但是cx_oracle成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆