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' )
#第二种 #第三种 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/