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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐