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')
相关推荐
咩图13 小时前
VSCode+Python创建项目
开发语言·python
zhanglu511613 小时前
Java Lambda 表达式使用深度解析
开发语言·前端·python
Coding茶水间13 小时前
基于深度学习的车牌识别系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
开发语言·人工智能·深度学习·yolo·机器学习
Hello.Reader13 小时前
Flink Python REPL(pyflink-shell)实战:本地/远程/YARN 三种启动方式 + Table API 交互开发流程
python·flink·交互
henry10101013 小时前
利用Python一键创建AWS EC2实例
linux·python·云计算·aws·ec2
EveryPossible13 小时前
工作流练习
服务器·python·缓存
一次旅行13 小时前
接口自动化测试模板
数据库·python·pytest
Suryxin.13 小时前
从0开始复现nano-vllm「model_runner.py」上半篇之初始化分布式推理环境
人工智能·python·深度学习·机器学习·vllm
奔跑的蜗牛FelixChioa13 小时前
python学习之快速掌握 pandas 数据可视化:「matplotlib+seaborn」极简实战方案
python·pandas·数据可视化
郁闷的网纹蟒13 小时前
虚幻5---第15部分---宝藏(掉落物)
开发语言·c++·ue5·游戏引擎·虚幻