python连接oracle

python连接oracle,第1张

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

②安装客户端
根据服务端的位数版本安装客户端
我本地安装客户端的地址为: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'] = 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()     #关闭数据库连接


之所以加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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/916255.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存