Python 连接 Oracle 详解

文章目录

  • 1 首先,安装第三方库 cx_Oracle
  • 2 其次,配置命令

1 首先,安装第三方库 cx_Oracle

  • 参考 CSDN 博客:Python 安装第三方库详解(含离线)

2 其次,配置命令

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)

执行结果:

连接信息:<cx_Oracle.Connection to scott@127.0.0.1:1521/orcl>
(10, 'ACCOUNTING', 'NEW YORK')
(20, 'RESEARCH', 'DALLAS')
(30, 'SALES', 'CHICAGO')
(40, 'OPERATIONS', 'BOSTON')

你可能感兴趣的:(Python,python,oracle,开发语言)