본문 바로가기

콩's EDUCATION/콩's Python

파이썬 오라클 데이터베이스 연동 방법

1. 파이썬을 데이터베이스와 연동!







2. 이제는 파이썬을 데이터베이스와 연동하는 방법에 대해서 설명하고자 합니다.


이제는 파이썬을 데이터베이스와 연동하는 방법에 대해서 설명하고자 합니다.


[참고사이트]

http://cx-oracle.readthedocs.io/en/latest/installation.html#quick-start-cx-oracle-installation

1. 오라클 instant Client 설치


http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html


본인 OS 및 bit에 맞는 정보를 확인하여 설치합니다.


글쓴이느 윈도우7 32bit 환경으로 아래 클라이언트를 설치합니다.


Instant Client Downloads for Microsoft Windows 32-bit


2. python 오라클 연동 모듈 cx_Oracle 설치


아래와 같이 명령어를 입력합니다.


python -m pip install cx_Oracle --upgrade


3. 환경변수 path 정보 입력


1번 zip 파일 압축을 해제하여 디렉토리를 생성하고 Oracle 12.2, 12.1 or 11.2 사용자는 환경 변수 path를 적용합니다.


환경 변수 적용은 검색을 통해서 금방 확인 가능합니다.


4. 테스트 합니다.


# myscript.py

from __future__ import print_function

import cx_Oracle

# Connect as user "hr" with password "welcome" to the "oraclepdb" service running on this computer.
connection = cx_Oracle.connect("hr", "welcome", "localhost/orclpdb")

cursor = connection.cursor()
cursor.execute("""
    SELECT first_name, last_name
    FROM employees
    WHERE department_id = :did AND employee_id > :eid""",
    did = 50,
    eid = 190)
for fname, lname in cursor:
    print("Values:", fname, lname)



3. 정리