Pyhton 连接 Oracle 的方法
小李的学习笔记
记本人借 Python 连接 Oracle 库的过程,本文借鉴CSDN各位博主的内容,整理了本人连接Oracle库的过程,以防本人遗忘,供各位参考。
1.准备工作
①.下载与电脑位数相匹配的instantclient 并解压文件
下载网址:https://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
②.Python 安装 cx_Oracle
安装命令:pip install cx_Oracle
#注意:instantclient 的位数应与 Python 位数相同,以避免后程连接 Oracle 库时报错Cannot locate a 64-bit Oracle Client library
2.配置环境
①.需要在环境变量中添加的环境:
NLS_LANG
SIMPLIFIED CHINESE_CHINA.UTF8
TNS_ADMIN
C:\Lee\instantclient-basic-windows.x64-19.10.0.0.0dbru.zip\instantclient_19_10
Path
C:\Lee\instantclient-basic-windows.x64-19.10.0.0.0dbru.zip\instantclient_19_10;
#注意:环境变量Path&TNS_ADMIN的地址为instantclient的文件存放地址
将instantclient文件下的oci.dll、oraocci11.dll、oraociei11.dll复制到Python安装文件下的site-packages
将instantclient文件下的所有.dll文件复制到Pyhton安装文件下
3.连接 Oracle 库:
Pyhton 连接 Oracle的代码:
import cx_Oracle as oracle
db = oracle.connect(‘用户名/密码@IP:端口/orcl’)
cursor = db.cursor()
cursor.execute(‘select *from table’)
data = cursor.fetchone() #取一条数据查看
print(data)
cursor.close()
db.close()
!!祝我中鞋!!