python连接oracle数据库

python连接oracle数据库

 

安装模块

使用第三方模块 `cx_Oracle`

1
pip install cx_Oracle

 连接方式

1
2
3
4
5
6
7
8
9
import  cx_Oracle as cx
 
#第一种
con  =  cx.connect( 'root' 'root123' '127.0.0.1:1521/orcl' )
#第二种
con  =  cx.connect( 'root/root123@127.0.0.1:1521/orcl' )
#第三种
dsn  =  cx.makedsn( '127.0.0.1' '1521' 'orcl' )
connection  =  cx.connect( 'root' 'root123' , dsn)

 简单使用

1
2
3
4
5
6
7
8
9
# -*- coding: utf-8 -*-
import  cx_Oracle as cx       #导入模块
con  =  cx.connect( 'root' 'root123' '127.0.0.1:1521/ORCL' )   #创建连接
cursor  =  con.cursor()        #创建游标
cursor.execute( "select * from TDER where ID='28'" )   #执行sql语句
data  =  cursor.fetchone()         #获取一条数据
print (data)      #打印数据
cursor.close()   #关闭游标
con.close()      #关闭数据库连接

 oracle数据库的操作和mysql的pymysql操作基本相同,可以参考pymysql使用

windows下使用注意

windows下是使用需要安装`Instant Client`

1) 下载 Instant Client

地址:http://www.oracle.com/technetwork/topics/winx64soft-089540.html

 

2) 解压到一个目录,并添加到环境变量

 

其他系统下使用也需要安装这个工具,具体可以查看此文档:

  https://oracle.github.io/odpi/doc/installation.html#windows

cx_Oracle官网:https://oracle.github.io/python-cx_Oracle/

转载于:https://www.cnblogs.com/baili-luoyun/p/11316255.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值