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')
相关推荐
风送雨2 分钟前
八周Python强化计划(七)
开发语言·python
ππ很开心6665 分钟前
DAY 32 函数专题2:装饰器
开发语言·python
Knight_AL9 分钟前
阿里《Java 开发手册》下的对象构建与赋值规范实践
java·开发语言
lsx20240611 分钟前
SQL LIKE 操作符详解
开发语言
微爱帮监所写信寄信18 分钟前
微爱帮监狱寄信写信工具照片高清处理技术架构
开发语言·人工智能·网络协议·微信·php
山沐与山18 分钟前
LangChain Tools解析:让Agent拥有超能力
人工智能·python·langchain
TonyLee01720 分钟前
python代码运行时间信息记录
python
曲幽26 分钟前
手把手搞定FastAPI静态文件:安全、上传与访问
css·python·fastapi·web·js·favicon·staticfiles
sandwu27 分钟前
AI Agent——可观测性链路集成&评测体系搭建(Langfuse)
人工智能·python·langchain·langfuse
报错小能手29 分钟前
数据结构 字典树
开发语言·数据结构