LoginSignup
0
0

More than 1 year has passed since last update.

Python Thin Driver for Oracle Database を試す

Last updated at Posted at 2022-05-26

はじめに

こちらのサイト「Open Source Python Thin Driver for Oracle Database 」にてPython言語に対するOracle Database接続のThin Driverがリリースされたと発表がありましたので試してみました。

Oracle Database への言語別アクセスドライバについては nakaie さんのこちらのまとめ記事「Oracle Databaseへの言語別アクセスドライバのまとめ」をご確認ください。

これまで Python から Oracle Databaseへアクセスするには cx_oracle というアクセスドライバが必要で、これにはOracle Instant Client といったOracle Clientも一緒に用意する必要があります。
Oracle Client製品の用意が不要となり、利用者にとって非常に便利になりました。

代表的なアプリケーション開発言語とミドルウェアの関係

前回の記事で以下のような図を提示しました。
20220526_oracleclient_old.png

これをこちらのように変更いたしました。
20220526_oracleclient_new2.png

Oracle Objects for OLEなど、あまりにも古い記述は削除しております。(こちらの資料をUpdateしたのは何年ぶりだろう。。)

(2022/05/26 15:30追記: nakaieさんにThick mode についてご指摘いただきましたので図を修正いたしました。nakaieさん、ありがとうございました。)

試したこと

  1. Oracle Client (Oracle Instant Client含む)をインストールせずにOracle Databaseへ接続できること
  2. (これまで、ことあるごとに気にしていた)Oracle Autonomous Database 共有Exadata Infrastructure (ADB-S) への接続において Wallet無し接続ができること

上記は同時に試すことができるので一度に実施することとします。
また、結論としては問題なくできました。

試した内容

こちらの構成を用意しました。
20220526_oci構成.png

検証の流れは以下の通りとなります。すべて用意した Oracle Linux 8サーバでの作業です。
なおoracledbドライバのインストールの詳細はこちらをご確認ください。

  1. Python のバージョン確認(3.6であり、サポート対象のバージョンのためそのまま利用)
  2. pip3 を最新のものにアップデート (9.0.3 ⇒ 21.1.1)
  3. Pythonの仮想環境で oracledb をpip3インストール
  4. テスト用のスクリプトを実行

以下は上記に関する補足です。
pip3 (バージョン9.0.3)ではドライバーのインストール時に以下のようなエラーが発生しました。これは上述の通りpip3を新しいもの(バージョン21.1.1)にアップデートするだけで解消しました。

(oracledb) [opc@muapex01 ~]$ python -m pip install oracledb --upgrade
Collecting oracledb
  Using cached https://files.pythonhosted.org/packages/a3/8d/54e6f761474ca648e2a493ef39722fff46cb46121e78df8ac0fe8fe621d2/oracledb-1.0.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Collecting cryptography>=3.4 (from oracledb)
  Using cached https://files.pythonhosted.org/packages/51/05/bb2b681f6a77276fc423d04187c39dafdb65b799c8d87b62ca82659f9ead/cryptography-37.0.2.tar.gz
    Complete output from command python setup.py egg_info:

            =============================DEBUG ASSISTANCE==========================
            If you are seeing an error here please try the following to
            successfully install cryptography:

            Upgrade to the latest pip and try again. This will fix errors for most
            users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip
            =============================DEBUG ASSISTANCE==========================

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-s59g4ey5/cryptography/setup.py", line 14, in <module>
        from setuptools_rust import RustExtension
    ModuleNotFoundError: No module named 'setuptools_rust'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-s59g4ey5/cryptography/

テスト用のスクリプトはインストールガイドのものを少し改変しています。
別途、環境変数にATPへの接続用のユーザ、パスワード、接続文字列を用意しています。


# test.py

import oracledb
import os

un = os.environ.get('PYTHON_USERNAME')
pw = os.environ.get('PYTHON_PASSWORD')
cs = os.environ.get('PYTHON_CONNECTSTRING')

with oracledb.connect(user=un, password=pw, dsn=cs) as connection:
    with connection.cursor() as cursor:
        sql = """select username from user_users"""
        for r in cursor.execute(sql):
            print(r)

実行内容は以下の通り。

(oracledb) [opc@muapex01 ~]$ python test.py
/home/opc/oracledb/lib64/python3.6/site-packages/oracledb/connection.py:38: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
  from . import base_impl, thick_impl, thin_impl
('ADMIN',)

20220526_python_oracledb.png

最後に

Python のような動的型付け言語で Thin Driver が提供されたことにとても驚きました。

なお、こちらのBlogでいちばんお話したかった点は、「代表的なアプリケーション開発言語とミドルウェアの関係」の説明用の図をUpdateしたことです(笑)。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0