python3连接oracle数据库

声明:python,cx_Oracle和instantclient的版本应一致

我这里使用的版本是python3.6 64位 ,cx_Oracle-5.3-11g.win-amd64-py3.6-2和instantclient-basic-windows.x64-18.5.0.0.0dbru

1. 首先安装cx_Oracle包

尽量不要直接使用pip install cx_Oracle,这样默认安装的是最新版本的cx_Oracle,可能会出现以下错误

1.1 cx_Oracle 报错:cx_Oracle.DatabaseError: DPI-1050: Oracle Client library must be at version 11.2

解决方法:https://pypi.python.org/pypi/cx_Oracle/5.3 下载低版本cx_Oracle版本 可以下载cx_Oracle-5.3-11g.win-amd64-py3.6-2.exe ,然后直接安装

1.2 如果在安装cx_Oracle-5.3-11g.win-amd64-py3.6-2.exe 时提示Python version 3.6 required, which was not found in the registry

可以通过执行这个文件来解决

register.py   链接: https://pan.baidu.com/s/1GcPK_I7ddSLZkM2sv7AHtA    提取码: qrwm 

2. 下载instantclient-basic-windows.x64-11.2.0.4.0.zip,解压并配置环境变量(放到path中)

下载地址:https://www.oracle.com/technetwork/cn/topics/winx64soft-101515-zhs.html

下载好后解压,并配置环境变量

3. 将instantclient下所有.dll文件到python\Lib\site-packages\下(我这里的路径为C:\Develop\Anaconda3\Lib\site-packages)

4. 测试代码如下

import cx_Oracle
conn=cx_Oracle.connect('username','password','172.16.5.29:1521/ORCL')
cursor=conn.cursor()

print("连接成功!")

cursor.close()
conn.commit()
conn.close()

当你看到“连接成功”的提示语句时,那么就恭喜你成功了用python连上了oracle数据库

posted @ 2019-06-29 13:25  Workaholic  阅读(18290)  评论(0编辑  收藏  举报