-
Python에서 Oracle 연결하기Backend/Oracle 2023. 6. 5. 15:07
필자는 전처리 후, csv 파일을 DB에 insert 하려고 하는데, 계속 여러 오류가 났었다.
오류 처리를 대체 어떻게 하면 좋을지, 막막하던 도중 파일을 한줄씩 insert 해서 오류가 나는 것은 로그로 남기기로 했다.
이때, 먼저 Oracle DB를 연결하기로 했다.
필자는 DBeaver 을 이용하였으며, DBeaver 설치 부터 시작됨!
1. DBeaver 를 먼처 설치함 -> 아래 링크에서 download를 진행 (os에 맞춰서 설치할 것)
2. 이 후 oracle 계정이 있다면, oracle 계정을 따로 기록해둔다.
3. 그리고 파이썬에서 DB에 연결할 수 있는 오라클 인스턴스 클라이언트를 설치한다. -> cx_Oracle
설치 방법 참고 url : https://cubenuri.tistory.com/202
python3 cx_Oracle 설치
파이썬으로 오라클 접속해야하는 상황이라 이것저것 해보다가 갑자기 된 상황이라 왜 된건지.... -_-;; 암튼 일단 실제 적용했던것을 기록으로 남김다. OS : 윈도우 10 64bit 1. 오라클 인스턴트 클라
cubenuri.tistory.com
import pandas as pdimport numpy as npimport json as jsimport datetimeimport csvfrom tqdm.auto import tqdmimport cx_Oracle as cximport loggingimport datetime
#oracle 설정host_name = 'localhost2'oracle_port = 1521service_name = 'xe'
dsn = cx.makedsn(host_name, oracle_port)conn = cx.connect('user', 'password')cursor = conn.cursor()
# oracle db 연결하기def ExecuteQuery(Query):try:dsn = cx.makedsn(host_name, oracle_port)conn = cx.connect('user', 'password')cursor = conn.cursor()cursor.execute(Query)cursor.close()conn.commit()conn.close()return Trueexcept Exception as e:logging.error(RuntimeError(f'[ERROR][ExecuteQuery] : {Query} {e}'))return False4. 설치를 완료 했다면, 파이썬에 적용하기
필자의 경우, executequery를 함수화 하였음 왜냐하면 csv내 데이터를 한줄씩 insert 할 예정이기 때문임!
이 후 전체적인 파이프라인은 다음에서!
'Backend > Oracle' 카테고리의 다른 글
MySQL Community Server 8.0.34 설치하기 (0) 2023.09.21 파이썬 데이터 전처리 후 oracle insert 하기 (0) 2023.06.10 Python 통한 데이터 전 처리 후, ORACLE DB에 Insert 할 때의 오류 (0) 2023.06.05 Oracle 오류 로그 정리 (0) 2023.06.05