LoginSignup
22
26

More than 5 years have passed since last update.

Windows環境のPythonからOracleに接続してみる

Last updated at Posted at 2018-06-02

前提はWindows7環境にAnaconda5.2-Python 3.6 versionがインストールしている状態からの手順です。

PythonからOracleに接続するには
cx_Oracle、Oracle Clientをインストールします。

cx_Oracle要件確認

Python 2.7 or 3.4 and higher. Older versions of cx_Oracle may work with  older versions of Python.

Pythonのバージョンは、3.6.4なのでOK!

Oracle client libraries. These can be from the free Oracle Instant Client,or those included in Oracle Database if Python is on the same machine as the database.Oracle client libraries versions 12.2, 12.1 and 11.2 are supported on Linux, Windows and macOS.Users have also reported success with other platforms.

以前にOracle Client をインストールしているのでOK!

An Oracle Database. Oracle’s standard client-server version interoperability allows cx_Oracle to connect to both older and newer databases.

これはClientとDatabaseの接続が問題ないかを言っている
ClientのバージョンとDBのバージョンがサポートされる組み合わせですよねと言う事

My Oracle Suppot
=====
クライアント / サーバー 異なるバージョン間の互換性サポート・マトリクス (ドキュメントID 2127402.1)
=====

自分の環境はOracle Clientが11g 、接続DBは12c、11gなのでOK!

cx_Oracleのインストール

Anaconda Promptを起動

image.png

Anaconda Promptで
cx_Oracleを 「 python -m pip install cx_Oracle --upgrade 」 でインストール

(base) C:\Users> python -m pip install cx_Oracle --upgrade

Collecting cx_Oracle
  Downloading https://files.pythonhosted.org/packages/82/75/58333eae77a58a7243cc
6905c8dfec6ef92ab5d4b6c33d0c70d1b7f3ce3a/cx_Oracle-6.3.1-cp36-cp36m-win_amd64.wh
l (162kB)
    100% |████████████████████████████████| 163kB 168kB/s
notebook 5.4.0 requires ipykernel, which is not installed.
jupyter 1.0.0 requires ipykernel, which is not installed.
jupyter-console 5.2.0 requires ipykernel, which is not installed.
ipywidgets 7.1.1 requires ipykernel>=4.5.1, which is not installed.
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-6.3.1 

「Successfully installed cx-Oracle-6.3.1」 とでたので
ためしに cx_Oracleをインポートしてみる

Anaconda Promptを起動 して import cx_Oracle を実行

(base) C:\Users>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bi
t (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import cx_Oracle
>>> cx_Oracle.clientversion()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded
: "C:\app\product\11.2.0\client_1\bin\oci.dll is not the correct archit
ecture". See https://oracle.github.io/odpi/doc/installation.html#windows for help

なにやらエラー!
メッセージをみると 自分のPCにインストールしたPython は64bit 、以前にインストールしたOracle Client は32bit なのでエラーになっていて、対応は64bitのOracle Clientをいれないといけないようだ

Oracle Instanct Clientをダウンロード

ここから instantclient-basic-windows.x64-11.2.0.4.0.zip をダウンロードして
任意の場所に保存して、unzip する

Windows環境変数を設定

環境変数 PATHに
Oracle Instanct Clientを解凍したパスを追加する
[コントロールパネル]-[システム]-[システム詳細設定]-[環境変数]で設定

再度、トライ

(base) C:\Users>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bi
t (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import cx_Oracle
>>> cx_Oracle.clientversion()

(11, 2, 0, 4, 0)

さっき、ダウンロードしたOracle Instance Client のバージョンが表示された!

Pythonのコードから接続してみる

oraconnect.py
import cx_Oracle

#サーバ名 / IP
HOST = "xxxxxx"
#ポート
PORT = 1521
#SID
SID = "orcl"


tns = cx_Oracle.makedsn(HOST, PORT, SID)
conn = cx_Oracle.connect("scott", "tiger", tns)

cur = conn.cursor()
cur.execute("""SELECT INSTANCE_NAME,HOST_NAME FROM v$instance""")

rows = cur.fetchall()

for r in rows:
    print("%d : %s" % (r[0], r[1]))

実行結果

orcl:xxxxxx

インスタンス名とホスト名が表示された!

上記コードは
Oracle の SID を指定しています。

Oracle RAC の場合、SERVICE_NAME を指定することが多いので
SERVICE_NAMEの指定方法は以下の投稿を参照ください。

関連リンク

Pythonスクリプトの実行方法、バインド変数を使用したSQL、SELECT文のデータ取得方法については
以下を参照ください

Windows環境でPythonスクリプト(.py)の実行方法

Python Oracleでバインド変数を使ったSQLを実行する

Python Oracle SQL(select文)データ取得方法(fetchall、fetchmany、fetchone)とチューニングパラメータ

22
26
2

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
22
26