Log.log(work)

いろんな作業メモ

オラクルにつないでデータを取得する

仕事で表題のスクリプトを作成することになったのでその時のメモ。

環境:
Windows8.1 (64bit) /
Anaconda (Python 3.6.4) / Oracle 11g

Oracleに接続

cx_Oracleを使うと良いらしい。ということでOracle Clientも併せてインストール

※Anadondaのpipが古い場合はバージョンアップしておく

cx_Oracleのインストール

python -m pip install cx_Oracle --upgrade

参考:
GitHub - oracle/python-cx_Oracle: Python interface to Oracle Database conforming to the Python DB API 2.0 specification.

Oracle Instanct Clientのインストール

Instant Client for Microsoft Windows (x64) 64-bit

Pythonから接続

import cx_Oracle
conn = cx_Oracle.connect(user='user', password='pass', dsn='localhost:port/xe')
cur = conn.cursor()

UnicodeDecodeError

接続先のOracle11gはshift-jis(JA16SJISTILDE)なのでPythonで扱えない文字が入っているとエラーになる。
解決方法が見つからない…

追記

UnicodeDecodeErrorはおまじないを唱えておけば大丈夫そう。

# -*- coding:utf-8 -*-

import cx_Oracle
import os

os.environ["NLS_LANG"] = "JAPANESE_JAPAN.JA16SJISTILDE" #おまじない