python连接oracle

①首先查看数据库服务端的版本:
查询的SQL:SELECT * FROM V$VERSION
image

②安装客户端
根据服务端的位数版本安装客户端
我本地安装客户端的地址为:D:\PLSQL\instantclient_11_2\instantclient_11_2

③python安装cx_Oraclel
命令:pip install cx_Oraclel

④连接数据的代码

点击查看代码
import os
import cx_Oracle as cx      #导入模块
'''
之所以加os.environ['path']这个是因为我oracle是64位的,python是32位的,装的cx_Oraclel也是32,不加这句就会报"x_Oracle.DatabaseError: DPI-1047: Cannot locate a 32-bit Oracle Client library: "The specified module could not be found"的错误,所以这里我把数据库连接指向我客户端的安装路径,而非python中的cx_Oraclel
'''
os.environ['path'] = r'D:\PLSQL\instantclient_11_2\instantclient_11_2'
con = cx.connect('用户', '密码', '数据库地址')  #创建连接
cursor = con.cursor()       #创建游标
cursor.execute("select * from pat_patient where code='101207'")
data = cursor.fetchone()        #获取一条数据
#cur.fetchmany(3) #获取前3条数据
#cur.fetchall() #获取所有数据
print(data)     #打印数据
cursor.close()  #关闭游标
con.close()     #关闭数据库连接


posted @ 2022-05-09 16:49  sriver_dl  Views(130)  Comments(0Edit  收藏  举报