Python 连接 Oracle 详解

文章目录

  • [1 首先,安装第三方库 cx_Oracle](#1 首先,安装第三方库 cx_Oracle)
  • [2 其次,配置命令](#2 其次,配置命令)

1 首先,安装第三方库 cx_Oracle

2 其次,配置命令

python 复制代码
import cx_Oracle

# 1.数据库连接的基本信息
user = 'scott'
password = 'scott'
host = '127.0.0.1:1521'
service_name = 'orcl'
connect_str = f'{user}/{password}@{host}/{service_name}'  # 格式化

# 2.通过 cx_Oracle 模板连接 Oracle 数据库
connect = cx_Oracle.connect(connect_str)
print(f'连接信息:{connect}')

# 3.通过游标 cursor 执行 sql
cursor = connect.cursor()
cursor.execute('select * from scott.dept')
for i in cursor.fetchall():
    print(i)

执行结果:

sql 复制代码
连接信息:<cx_Oracle.Connection to scott@127.0.0.1:1521/orcl>
(10, 'ACCOUNTING', 'NEW YORK')
(20, 'RESEARCH', 'DALLAS')
(30, 'SALES', 'CHICAGO')
(40, 'OPERATIONS', 'BOSTON')
相关推荐
AuroraWanderll5 分钟前
深入理解C++多态(三):多态的原理——虚函数表机制(上)
c语言·开发语言·数据结构·c++·算法·stl
lly2024069 分钟前
Python Number(数字)
开发语言
阿沁QWQ10 分钟前
STL库vector模拟实现
开发语言·c++
未来之窗软件服务11 分钟前
操作系统应用(三十二)python版本选择系统—东方仙盟筑基期
开发语言·python·东方仙盟·操作系统应用
Ustinian_31015 分钟前
【python】图片转PDF工具【附完整源码】
开发语言·python·pdf
数智化架构师-Aloong17 分钟前
⚡️ PowerJob深度解析:Java生态下高并发分布式调度的终极选择
java·开发语言·分布式·系统架构
棒棒的皮皮17 分钟前
【OpenCV】Python图像处理之读取与保存
图像处理·python·opencv
Godson_beginner42 分钟前
Sa-Token (java权限认证框架)
java·开发语言
谢景行^顾44 分钟前
numpy
开发语言·python·numpy