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')
相关推荐
小成202303202654 小时前
Linux高级02
linux·开发语言
知行合一。。。4 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y4 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
咸鱼2.04 小时前
【java入门到放弃】需要背诵
java·开发语言
ZK_H4 小时前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐4 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子4 小时前
Java:异常(exception)
java·开发语言
lifewange4 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium275 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
cmpxr_5 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法