环境
win10
python3.7
oracle11g
安装依赖
1.安装cx_Oracle
pip install cx_Oracle
2.安装oracle客户端
1、去官网下载 64 位 Windows 客户端,选择【instantclient-basic-windows.x64-11.2.0.4.0.zip】下载。
https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
2、配置环境变量
将解压后的文件路径追加到PATH中
D:\Oracle\instantclient_11_2
3.在解压的文件夹路径下创建一个名为“tnsnames.ora”的文件,内容如下。
orcl =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 119.254.155.148)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = PDBORCL)
)
)
4.将解压文件夹下的oci.dll, oraocci12.dll,oraociei12.dll 拷贝到python安装目录下的lib/site-packages中。
3.在控制台中或者pycharm中验证是否能连接成功
输入下边代码不报错则代表环境没问题了。
import cx_Oracle
conn = cx_Oracle.connect('username','password','ip:port/server name')
or
conn = cx_Oracle.connect('username/password@ip:port/server name')
4.解决查询汉字出现乱码的问题
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
or
os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'