一、添加python环境变量及安装cx_Oracle
在windows下载好python3.6并安装好后,进入在运行中输入cmd,然后输入python不能运行表示需要添加环境变量,步骤如下:
1.我的电脑=>属性=>高级系统设置=>高级=>环境变量=>系统变量=>Path,点击编辑,把python的路径添加进去
2.同样的步骤,把python的pip路径添加进去
例如我的path是:%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Users\Administrator\Desktop;C:\Users\Administrator\AppData\Local\Programs\Python\Python36;C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts;
3.在cmd界面输入 pip install cx_Oracle
结果显示 Successfully installed cx-Oracle-6.3.1 就表示安装成功
二、环境配置
1.这时候连接Oracle的数据库
报错,原因是没有配置运行Oracle的环境变量,这时候需要去官网下载相关的文件:http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html,找到和自己电脑相匹配的版本后下载(ps:需要自己在oracle创建账户,步骤挺简单的),参考资料:https://blog.csdn.net/qq_38307594/article/details/78163638
2.把下载的文件解压,把解压的文件路径放入系统环境变量中,并且把解压的文件目录中oraocci11.dll、oraociei11.dll、oci.dll(要连接的oracle版本是11)复制到 python安装目录 \Lib\site-packages\,这时候重新连接数据库
网上下载一个缺失的文件,解压后把“MSVCR120.dll”放进我的电脑C盘->windows->System32
这样重新打开python,连接数据库,结果如下:
我的系统环境变量:%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Users\Administrator\Desktop;C:\Users\Administrator\AppData\Local\Programs\Python\Python36;C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts;C:\Users\Administrator\AppData\Local\Programs\Python\Python36\instantclient_11_2;
总的来说按照叙述流程走的话,基本能够实现python连接cx_Oracle
三、Oracle连接其它问题
oracle 6.3和5.3版本还是有点差别的,例如当DataFrame数据中nan传入oracle时,当列数据类型为int/float时,cx_Oracle 5.3版会自动转换为0,而6.3版则会报错,需要用fillna()替换nan值
1.运行cx_Oracle 6.3
Windows中在cmd界面用 pip install cx_Oracle 直接安装的是6.3版,在运行程序时报错:'UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)',这是数据中的中文oracle不能识别,需要在表头加上
import os
os.environ['NLS_LANG'] = ".AL32UTF8"
2.卸载cx_Oracle 6.3
cmd中运行命令
pip uninstall cx_Oracle
报错'PermissionError: [WinError 5] 拒绝访问',虽然在pip list中看不到cx_Oracle,但是卸载不干净,这时候cmd输入命令
pip install -U wheel
py -3.6 -m pip install --upgrade pip
重新安装次再卸载就没有报错了
3.安装cx_Oracle 5.3
想安装5.3版本的cx_Oracle,cmd中输入命令:
python -m pip install cx-oracle==5.3
报错:'Command "python setup.py egg_info" failed with error code 1 in C:\Users\ADMINI~\AppData\Local\Temp\pip-build-rkk6oflb\cx-oracle\'
没办法,只好去官网下载版本5.3: https://www.lfd.uci.edu/~gohlke/pythonlibs/ 找到cx_Oracle 5.3对应版本下载到本地桌面,然后安装
cd desktop
pip install cx_Oracle-5.3+oci12c-cp36-cp36m-win32.whl
4.安装cx_Oracle 环境依赖包
这时候在python输入
import cx_Oracle
报错:ImportError: DLL load failed: 找不到指定的模块。
但是之前cx_Oracle 6.3都能都运行,在python下的Lib\site-packages 中instantclient 12.2.0.1.0 的oci.dll也存在(5.3版本配置好oci.dll可以不用配置PATH环境变量);
检查版本问题:Windows、python、instantclient都是64位操作系统,没有问题;
再看依赖环境,已经安装过了Microsoft Visual Studio 2008 Redistributable、Microsoft Visual Studio 2015 Redistributable,这时候对比instantclient 12.2.0.1.0安装要求,原来它需要Microsoft Visual Studio 2013 Redistributable,在官网下载后安装,在python终于能够import cx_Oracle成功
5.pandas问题
python中运行
import pandas
报错:AttributeError: module 'pandas' has no attribute 'core'
猜想这是pandas版本太高,因为在pandas 0.21.0的老版本中能够运行,直接把pandas卸载,重新安装个低版本的
pip install pandas==0.21.0
6.丢失文件MSVCR100.dll
连接数据库的时候报错:计算机丢失文件MSVCR100.dll 。 真的感觉很坑,为了使用cx_Oracle 5.3 ,报这么多错误
网上下载一个对应的文件放进我的电脑C盘->windows->System32
7.cx_Oracle.InterfaceError
cx_Oracle连接Oracle时报错: Unable to acquire Oracle environment handle。 这是 Lib\site-packages缺少几个dll,把instantclient 12.2.0.1.0 中的 oraocci12.dll、oraociei12.dll、oci.dll、oraons.dll移到site-packages内(这和连接的oracle版本没有关系,只需要和cx_Oracle版本对应好就行)。
最后python中模块cx_Oracle 5.3程序终于能运行,真的是好事多磨。