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')
相关推荐
大傻^4 分钟前
SpringAI2.0 Null Safety 实战:JSpecify 注解体系与 Kotlin 互操作
android·开发语言·人工智能·kotlin·springai
sz-lcw7 分钟前
HOG特征向量计算方法
人工智能·python·算法
ZTLJQ8 分钟前
驾驭高并发:Python协程与 async/await 完全解析
服务器·数据库·python
魑魅魍魉都是鬼18 分钟前
Java 适配器模式(Adapter Pattern)
java·开发语言·适配器模式
笨笨马甲19 分钟前
Qt MQTT
开发语言·qt
一勺菠萝丶25 分钟前
芋道框架 - API 前缀区分机制
java·linux·python
kcuwu.26 分钟前
Python判断及循环
android·java·python
Maverick0626 分钟前
02-SQL执行计划与优化器:Oracle是怎么决定“该怎么查“的
数据库·sql·oracle·ffmpeg
前进的李工32 分钟前
LangChain使用之Model IO(提示词模版之ChatPromptTemplate)
java·前端·人工智能·python·langchain·大模型
Fairy要carry38 分钟前
面试-Agent上下文过载、步骤混乱的问题
开发语言·python