You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用oracledb与Pandas连接Oracle数据库报错求助

Python连接Oracle并导入Pandas DataFrame的问题

背景

我是Python新手,对Pandas完全陌生,现在需要连接公司本地DEV Oracle数据库,用Python+Pandas读取数据,选用了oracledb包。

当前可正常运行的代码

在VS Code的.ipynb文件中,以下代码能成功连接数据库:

# python -m pip install --upgrade pandas
import oracledb
import pandas as pd
from sqlalchemy import create_engine

connection = oracledb.connect(user="TEST", password="TESTING", dsn="TESTDB:1234/TEST")

print("Connected")
print(connection)

接着执行查询测试代码,能正常返回数据元组:

cursor=connection.cursor()
query_test='select * from dm_cnf_date where rownum < 2'

for row in cursor.execute(query_test):
    print(row)

遇到的问题

问题1:直接用oracledb连接对象传入pd.read_sql报错

执行代码:

df = pd.read_sql(sql=query_test, con=connection)

出现警告:

:1: UserWarning: pandas only supports
SQLAlchemy connectable (engine/connection) or database string URI or
sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please
consider using SQLAlchemy. df = pd.read_sql(sql=query_test,
con=connection)

问题2:改用SQLAlchemy引擎连接报错

参考文档改写代码后:

conn_url="oracle+oracledb://TEST:TESTING@TESTDB:1234/TEST"
engine=create_engine(conn_url)

df = pd.read_sql(sql=query_test, con=engine)

出现错误:

OperationalError: DPY-6003: SID "TEST" is not
registered with the listener at host "TESTDB" port
1234. (Similar to ORA-12505)

需求

仅需实现连接Oracle数据库并将查询结果导入Pandas DataFrame,求可行解决方案。


内容的提问来源于stack exchange,提问作者chilly8063

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:03:17