ITPub博客

首页 > 数据库 > Oracle > python中使用oracledb模块连接oracle数据库

python中使用oracledb模块连接oracle数据库

原创 Oracle 作者:ilsyx 时间:2022-10-24 09:38:46 0 删除 编辑

Python-oracledb 是 cx_Oracle 的新名字。目前oracledb支持Python versions 3.6 到 3.10


使用瘦模式时(不需要装oracle client)可以直接连接oracle database 12.1 以后的版本。 

使用胖模式时(需要安装oracle cient),由安装的oracle client版本决定,可以连接oracle  Oracle Database 9.2 以后的版本。


以windows安装oracledb为例:

使用pip工具安装 oracledb模块:

PS C:\Users\Administrator\PycharmProjects\pythonOracle> pip install oracledb --upgrade

Collecting oracledb

  Downloading oracledb-1.1.1-cp310-cp310-win_amd64.whl (1.1 MB)

     |████████████████████████████████| 1.1 MB 19 kB/s

Collecting cryptography>=3.2.1

  Using cached cryptography-38.0.1-cp36-abi3-win_amd64.whl (2.4 MB)

Collecting cffi>=1.12

  Using cached cffi-1.15.1-cp310-cp310-win_amd64.whl (179 kB)

Collecting pycparser

  Using cached pycparser-2.21-py2.py3-none-any.whl (118 kB)

Installing collected packages: pycparser, cffi, cryptography, oracledb

Successfully installed cffi-1.15.1 cryptography-38.0.1 oracledb-1.1.1 pycparser-2.21

WARNING: You are using pip version 21.3.1; however, version 22.3 is available.

You should consider upgrading via the 'C:\Users\Administrator\PycharmProjects\pythonOracle\venv\Sc

ripts\python.exe -m pip install --upgrade pip' command.

PS C:\Users\Administrator\PycharmProjects\pythonOracle> 


在python中,使用瘦模式以easy connect的方式连接数据的方式如下:

import oracledb


with oracledb.connect(user='system', password='oracle', dsn = '192.168.124.168:1522/R21300O') as connection:

    with connection.cursor() as cursor:

        sql = """select sysdate from dual"""

        for r in cursor.execute(sql):

            print(r)


(datetime.datetime(2022, 10, 24, 9, 36, 1),)


详细的参考文档可以查看:

https://oracle.github.io/python-oracledb/


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11780477/viewspace-2919898/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: OEL 8.6 卸载 21cRAC
请登录后发表评论 登录
全部评论

注册时间:2009-06-12

  • 博文量
    203
  • 访问量
    662411