使用 python-oracledb 连接 Oracle 数据库:解决安装难题

使用 python-oracledb 连接 Oracle 数据库:解决安装难题

本文旨在帮助读者解决在使用 python 连接 oracle 数据库时遇到的安装问题。由于 cx_Oracle 已被 pythonoracledb 取代,针对新版本 Python 的预编译二进制文件仅适用于 python-oracledb。本文将指导您如何正确安装和配置 python-oracledb,并介绍其 Thin 模式,该模式无需安装 Oracle 客户端库。

安装 python-oracledb

由于 cx_Oracle 已不再是首选方案,特别是对于较新版本的 Python,建议使用 python-oracledb。python-oracledb 提供了更好的性能和更多的功能。

要安装 python-oracledb,可以使用 pip 命令:

pip install python-oracledb

如果安装过程中遇到权限问题,可以尝试使用 –user 选项:

立即学习Python免费学习笔记(深入)”;

pip install --user python-oracledb

配置 python-oracledb

python-oracledb 提供了两种连接模式:Thick 模式和 Thin 模式。

  • Thick 模式: 需要安装 Oracle 客户端库,并且需要配置环境变量。
  • Thin 模式: 无需安装 Oracle 客户端库,直接连接 Oracle 数据库,更加方便快捷。

使用 Thin 模式 (推荐)

Thin 模式是 python-oracledb 的一个重要特性,它允许您直接连接 Oracle 数据库,而无需安装 Oracle 客户端库。这大大简化了配置过程。

要使用 Thin 模式,只需在连接字符串中指定数据库服务器的 IP 地址和端口号即可。

以下是一个使用 Thin 模式连接 Oracle 数据库的示例代码:

import oracledb  # 设置连接信息 username = "your_username" password = "your_password" dsn = "your_db_ip:1521/your_service_name" # 例如: 192.168.1.100:1521/ORCL  try:     # 建立连接     connection = oracledb.connect(user=username, password=password, dsn=dsn,  thick_mode=False)      # 创建游标对象     cursor = connection.cursor()      # 执行 SQL 查询     cursor.execute("SELECT * FROM your_table")      # 获取查询结果     results = cursor.fetchall()      # 打印结果     for row in results:         print(row)  except oracledb.Error as error:     print("Error occurred:", error)  finally:     # 关闭游标和连接     if cursor:         cursor.close()     if connection:         connection.close()

请务必将 your_username, your_password, your_db_ip, your_service_name 和 your_table 替换为您的实际数据库信息。 并且设置thick_mode=False。

使用 Thick 模式

如果需要使用 Thick 模式,则需要安装 Oracle 客户端库,并配置环境变量。

  1. 下载 Oracle 客户端库: 从 Oracle 官网下载与您的操作系统和 Oracle 数据库版本兼容的客户端库。
  2. 安装 Oracle 客户端库: 按照 Oracle 官方文档的指引安装客户端库。
  3. 配置环境变量: 将 Oracle 客户端库的安装目录添加到 PATH 环境变量中。 例如,在 windows 系统中,可以将 C:oracleinstantclient_19_14 添加到 PATH 环境变量中。 同时,可能还需要设置 TNS_ADMIN 环境变量,指向 tnsnames.ora 文件的目录。

以下是一个使用 Thick 模式连接 Oracle 数据库的示例代码:

import oracledb  # 设置连接信息 username = "your_username" password = "your_password" dsn = "your_db_ip:1521/your_service_name" # 例如: 192.168.1.100:1521/ORCL  try:     # 建立连接     connection = oracledb.connect(user=username, password=password, dsn=dsn)      # 创建游标对象     cursor = connection.cursor()      # 执行 SQL 查询     cursor.execute("SELECT * FROM your_table")      # 获取查询结果     results = cursor.fetchall()      # 打印结果     for row in results:         print(row)  except oracledb.Error as error:     print("Error occurred:", error)  finally:     # 关闭游标和连接     if cursor:         cursor.close()     if connection:         connection.close()

请务必将 your_username, your_password, your_db_ip, your_service_name 和 your_table 替换为您的实际数据库信息。

总结

使用 python-oracledb 连接 Oracle 数据库是一个相对简单的过程。 通过选择合适的连接模式(Thin 或 Thick),并按照上述步骤进行配置,您可以轻松地在 Python 代码中访问 Oracle 数据库。 推荐使用 Thin 模式,因为它无需安装 Oracle 客户端库,更加方便快捷。 如果在安装或配置过程中遇到问题,请参考 Oracle 官方文档或 python-oracledb 的文档。

以上就是使用

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享