• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
百里丶落云
Gee,gee,baby,baby
            管理     
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/

年与时驰,意与日去,遂成枯落, 多不接世,悲守穷庐,将复何及。
posted on 2019-08-07 16:46  百里丶落云  阅读(484)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3