MySQL 의 테이블 데이터를 CSV로 만들기

현장에서 운영업무를 하다 보면 현업이 Raw 데이터를 긴급으로 CSV로 달라는 요청이 종종 있다.

서버는 상용이고 터미널이외엔 적당한 tool이 없다.

 

이런 경우, Python을 활용하면 빨리 된다.

상용의 개인계정(리눅스)에 python 과 pip를 설치 할 수만 있다면 OK!

 

리눅스 서버에서,

pip install pandas
pip install sqlachemy

 

그리고 python 을 쳐서 python prompt 로 진입.

import pandas as pd
import sqlalchemy as sql

connect_string = 'mysql+pymysql://유저명:비밀번호@DB서버호스트명:3306/스키마명'

sql_engine = sql.create_engine(connect_string)
query =query = "select * from 주문테이블 where 조건들"
df = pd.read_sql_query(query, sql_engine)
df.to_csv(r'test.csv')

 

위와 같이 하면 총 7줄의 코딩으로  간단히 처리된다.

 

끝.

 

'Python' 카테고리의 다른 글

Python Celery Task Monitoring  (0) 2019.10.08
Python Lambda  (0) 2019.09.03
Celery from scratch  (0) 2019.05.20
Python - Monkey Patch  (1) 2019.05.02
Python Excel to MySQL  (0) 2019.04.12

+ Recent posts