헤르메스 LIFE

[Python] DB Connection - ORACLE 본문

Python

[Python] DB Connection - ORACLE

헤르메스의날개 2021. 3. 4. 01:05
728x90

Python 3.9


1. cx_Oracle 라이브러리 설치

(venv) C:\JetBrains\pythonProject\venv\Scripts>pip install cx_Oracle
Collecting cx_Oracle
Downloading cx_Oracle-8.1.0-cp39-cp39-win_amd64.whl (207 kB)
|████████████████████████████████| 207 kB 819 kB/s
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-8.1.0

1. Oracle Connection Sample #1

import cx_Oracle
#한글 지원 방법
import os
os.putenv('NLS_LANG', '.UTF8')

# 연결에 필요한 기본 정보 (유저, 비밀번호, 데이터베이스 서버주소:포트/SID)
connection = cx_Oracle.connect('hr', '1234', 'localhost:1521/XE')
cursor = connection.cursor()
print("Connection established")

# Drop previous table of same name if one exists
cursor.execute("SELECT * FROM TAB")

for x in cursor:
  print(x)

# Cleanup
#conn.commit()
cursor.close()
connection.close()
print("Done.")

 

728x90