【问题标题】:How do I connect oracle DB with my python script?如何将 oracle DB 与我的 python 脚本连接起来?
【发布时间】:2017-02-17 06:33:24
【问题描述】:

我需要将 oracle DB 与 python 连接并将它们转换为 Dataframe。想知道有哪些简单的软件包以及如何安装它们?

我正在使用 Anaconda 3(Python 3.5.2),我尝试使用 cx_oracle 但由于依赖关系而无法安装。听说过 SQLAlchemy 有点不知道怎么用。

【问题讨论】:

  • SQLAlchemy 没有自带驱动程序。你仍然需要cx_Oracle 之类的东西。您能否扩展此声明:“我尝试使用 cx_oracle 但由于依赖关系而无法安装”?解决这些问题可能是最好的方法。
  • 发生的确切错误是“ImportError: DLL load failed: %1 is not an valid Win32 application”。我已经安装了预编译版本 Cx_oracle。
  • 此时我将删除此问题并就该错误提出新问题。这样你会得到更多的关注。

标签: python anaconda ipython-notebook pandasql


【解决方案1】:

按照以下步骤,终于成功了,

1.从http://www.oracle.com/technetwork/topics/winsoft-085727.html下载并解压版本12。 2.添加“ORACLE_HOME”作为 Windows 环境变量,并将其值设置为 ...\instantclient_12_1,(不是它的包含文件夹!)。 3.将此相同的路径添加到“路径”环境变量。 4.现在才安装cx_Oracle。 5.重启系统

非常感谢大家!

【讨论】:

    【解决方案2】:

    我有使用 SQLAlchemy+MySQL 的经验。

    来自SQLAlchemy的文档,

    engine = create_engine('oracle://username:password@host:port/database') 是连接字符串。

    地点:

    username = 用户名

    password = 密码

    host = 主机名/IP 例如(127.0.0.1)

    port = 端口号

    database = 数据库名称

    engine = create_engine('oracle://username:password@host:port/database')
    sql = 'Your SQL query in string.'
    df = pd.read_sql(sql,con=engine) 
    

    这应该将 sql 响应读入数据帧。

    【讨论】:

    • 尝试抛出错误“没有名为 'cx_Oracle' 的模块”。正如我之前所说,由于依赖关系,我无法安装 cx_oracle。 Pip install 或 conda install 不起作用。
    • @prady,我的错。 SQLAlchemy 默认使用 cx_Oracle。
    猜你喜欢
    • 2015-06-26
    • 2016-09-08
    • 2021-09-03
    • 2016-01-12
    • 2021-06-04
    • 1970-01-01
    • 2013-02-27
    • 2021-03-18
    • 2022-01-20
    相关资源
    最近更新 更多