
파이썬으로 DB 명세서 엑셀 자동화하기 (1) - DB 접속하기
이미 운영되고 있는 서비스의 DB 명세서를 작성하려면 엄청난 시간이 들어갑니다 😢
DB에 접속하여 테이블 정보를 검색한 뒤 엑셀로 저장하는 프로그램을 작성해봅니다.
라이브러리
사용하는 DBMS에 맞는 라이브러리를 사용합니다.
- MySQL
PyMySQL https://github.com/PyMySQL/PyMySQL - Oracle
cx_Oracle https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html
DB 접속정보 정의
class DbConfig:
HOST = "192.168.xxx.xxx"
PORT = 1234
USER = "user"
PASSWORD = "password"
DATABASE = "db"
CHARSET = "utf8mb4" # MySQL
DB 접속
MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DB 연결 생성 | |
conn = pymysql.connect( | |
host=DB_CONFIG.HOST, | |
port=DB_CONFIG.PORT, | |
user=DB_CONFIG.USER, | |
password=DB_CONFIG.PASSWORD, | |
database=DB_CONFIG.DATABASE, | |
charset=DB_CONFIG.CHARSET, | |
cursorclass=pymysql.cursors.DictCursor, | |
) | |
# 반납해야 하는 자원 | |
with conn.cursor() as cursor: | |
# TODO - 테이블명 리스트 조회 |
Oracle
oracle client를 설치하고 해당 경로를 환경변수에 설정합니다.
https://www.oracle.com/kr/database/technologies/instant-client/winx64-64-downloads.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cx_Oracle | |
import os | |
# oracle client 경로 설정 | |
os.environ["PATH"] = "C:\\instantclient-basic-windows....;" + os.environ["PATH"] | |
# Oracle | |
conn = cx_Oracle.connect( | |
DB_CONFIG.USER, | |
DB_CONFIG.PASSWORD, | |
f"{DB_CONFIG.HOST}:{DB_CONFIG.PORT}/{DB_CONFIG.DATABASE}", | |
) | |
# 반납해야 하는 자원 | |
with conn.cursor() as cursor: | |
# TODO |
728x90
'Utils' 카테고리의 다른 글
엑셀에 데이터 입력하기 - DB 명세서 엑셀 자동화 (3) | Python (0) | 2025.01.10 |
---|---|
테이블, 컬럼 정보 조회하기 - DB 명세서 엑셀 자동화 (2) | Python (0) | 2025.01.06 |
IntersectionObserver를 이용한 scroll detector | Javascript (0) | 2024.10.30 |
랜덤 숫자 롤링 후 앞자리부터 맞추는 숫자 롤링 카운터 만들기 | HTML, Javascript, jQuery (0) | 2024.08.02 |
각 자릿수별로 1씩 증가하는 숫자 롤링 카운터 만들기 | HTML, Javascript, jQuery (0) | 2024.07.24 |