admin 發表於 2023-4-24 11:18:50

The python-oracledb and cx_Oracle Drivers

cx_ Oracle庫的最新版本已經升級為python -oracledb了;
By default, python-oracledb runs in a ‘Thin’ mode which connects directly to Oracle Database. This mode does not need Oracle Client libraries. However, some additional functionality is available when python-oracledb uses them. Python-oracledb is said to be in ‘Thick’ mode when Oracle Client libraries are used. see 23. Appendix A: Oracle Database Features Supported by python-oracledb — python-oracledb 1.3.1 documentation
python-oracledb驅動程序是一個Python擴展模塊,可以訪問Oracle數據庫。它具有支持Python數據庫API v2.0規範的全面功能,具有相當多的附加內容和一些排除項。

python-oracledb驅動程序是cx_Oracle 8.3的重命名的主要版本繼承者.

python-oracledb功能亮點:1)從PyPI 輕鬆安裝
2)支持多個Oracle 數據庫版本;支持Python 數據庫API v2.0 規範執行SQL 和PL/SQL 語句
3)廣泛的Oracle 數據類型支持,包括JSON 、大型對象( 和) 和SQL 對象的綁定CLOBBLOB連接管理,包括連接池Oracle 數據庫高可用性功能
4)充分利用Oracle 網絡服務基礎架構,包括加密的網絡流量
python-oracledb 與cx_Oracle 使用方法對比
cx_Oracle使用方法
import cx_Oracle# 设置连接参数
python-oracledb的使用方法
默認情況下,python-oracledb在“精簡”模式下運行,該模式直接連接到Oracle數據庫。此模式不需要Oracle 客戶端庫。但是,當python-oracledb使用它們時,可以使用一些其他功能。 Python-oracledb在使用Oracle客戶端庫時被稱為“Thick”模式。這兩種模式都具有支持Python 數據庫API v2.0 規範的全面功能。

從默認的精簡模式更改為Thick模式需要添加對oracledb.init_oracle_client() 的調用;當調用init_oracle_client() 時,python-oracledb 使用搜索啟發式方式動態加載Oracle Client 庫,使用方法:
import oracledb
獨立鏈接模式
import oracledbimport getpassuserpwd = getpass.getpass("Enter password: ")connection = oracledb.connect(user="hr", password=userpwd, dsn="dbhost.example.com/orclpdb")# 其他功能代码# 关闭连接connection.close()
或者可以使用以下方法:
username="hr"userpwd = os.environ.get("PYTHON_PASSWORD")host = "localhost"port = 1521service_name = "orclpdb"dsn = f'{username}/{userpwd}@{port}:{host}/{service_name}'connection = oracledb.connect(dsn)# 其他功能代码# 关闭连接connection.close()
使用連接池的方法
# 初始化连接pool = oracledb.create_pool(user="hr", password=userpwd, dsn="dbhost.example.com/orclpdb", min=2, max=5, increment=1)# Acquire 连接到池connection = pool.acquire()# 使用连接池with connection.cursor() as cursor: for result in cursor.execute("select * from mytab"): print(result)# 释放连接池pool.release(connection)# 关闭连接池pool.close()

頁: [1]
查看完整版本: The python-oracledb and cx_Oracle Drivers