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')
相关推荐
Howard在远程15 分钟前
[编程农场][The Farmer Was Replaced]——Carrot & Pumpkin
python
znhy@12327 分钟前
十三、JS进阶(二)
开发语言·前端·javascript
@木辛梓37 分钟前
Linux 线程
linux·开发语言·c++
PieroPc1 小时前
用python streamlit sqlite3 写一个聊天室
python·streamlit·聊天室
低头不见1 小时前
策略模式上下文管理
windows·python·策略模式
Xander W1 小时前
基于K8s集群的PyTorch DDP 框架分布式训练测试(开发机版)
人工智能·pytorch·分布式·python·深度学习·kubernetes
文火冰糖的硅基工坊1 小时前
[人工智能-大模型-103]:模型层 - M个神经元组成的单层神经网络的本质
python·算法·机器学习
坚持就完事了1 小时前
XPath语法及Python的lxml包学习
python
孤独的追光者1 小时前
使用Qt Designer开发上位机
开发语言·python·qt
杨超越luckly1 小时前
HTML应用指南:利用POST请求获取全国爱回收门店位置信息
大数据·前端·python·信息可视化·html