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')
相关推荐
itwangyang52010 分钟前
AIDD药物筛选与设计详细方法
人工智能·python
悟能不能悟17 分钟前
JAVA 对象转为二级制流,再转化为base64
java·开发语言
NiceAsiv22 分钟前
VSCode之打开python终端 取消conda activate的powershell弹窗
vscode·python·conda
蔚说32 分钟前
is 与 == 的区别 python
python
cnxy18836 分钟前
围棋对弈Python程序开发完整指南:步骤3 - 气(Liberties)的计算算法设计
python·算法·深度优先
进击的前栈1 小时前
Flutter跨平台网络图片缓存库cached_network_image鸿蒙化适配指导手册
开发语言·网络·rust
叶子2024221 小时前
骨架点排序计算
python
老华带你飞1 小时前
房屋租赁管理系统|基于java+ vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
AC赳赳老秦1 小时前
行业数据 benchmark 对比:DeepSeek上传数据生成竞品差距分析报告
开发语言·网络·人工智能·python·matplotlib·涛思数据·deepseek
小鸡吃米…1 小时前
带Python的人工智能——深度学习
人工智能·python·深度学习