데이터베이스 테이블을 JSON으로 저장하기
작업중에 필요해서 만든 코드
MySQL이나 MariaDB의 데이터를 활용해서 Json-Server용 데이터 파일을 생성하기 위해 작성한 스크립트.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import json
import pymysql
config = {
"host": "DB주소",
"port": 포트번호,
"user": "사용자이름",
"password": "비밀번호",
"dbname": "데이터베이스",
"charset": "문자셋(utf8mb4)",
"table": "테이블이름"
}
# MariaDB 연결 정보
conn = pymysql.connect(
host=config['host'], user=config['user'], password=config['password'],
port=config['port'], db=config['dbname'], charset=config['charset'])
cur = conn.cursor(pymysql.cursors.DictCursor)
# 전체 데이터 조회
cur.execute("SELECT * FROM %s;" % config['table'])
rows = cur.fetchall()
# JSON 파일로 저장
with open('%s.json' % config['table'], 'w', encoding='utf-8') as f:
json.dump(rows, f, ensure_ascii=False, indent=2,
default=lambda x: x if isinstance(x, int) or isinstance(x, float) else str(x)
)
cur.close()
conn.close()
This post is licensed under CC BY 4.0 by the author.