-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.py
More file actions
87 lines (74 loc) · 2.91 KB
/
insert_data.py
File metadata and controls
87 lines (74 loc) · 2.91 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import pymysql
from generate_data import generate_mock_data
# DB 연결 설정
conn = pymysql.connect(
host=os.getenv('DB_HOST'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
database=os.getenv('DB_NAME'),
charset='utf8mb4'
)
cursor = conn.cursor()
# 생성할 mock 데이터 개수 입력
count = int(input("테이블당 mock 데이터 개수: "))
data = generate_mock_data(count)
# 1. company
for c in data['company']:
cursor.execute("""
INSERT INTO company (id, name, created_at)
VALUES (%s, %s, %s)
""", (c['id'], c['name'], c['created_at']))
# 2. admin
for a in data['admin']:
cursor.execute("""
INSERT INTO admin (id, company_id, email, password, phone, role, status)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""", (a['id'], a['company_id'], a['email'], a['password'], a['phone'], a['role'], a['status']))
# 3. item
for i in data['item']:
cursor.execute("""
INSERT INTO item (id, company_id, name, total_quantity, available_quantity, status)
VALUES (%s, %s, %s, %s, %s, %s)
""", (i['id'], i['company_id'], i['name'], i['total_quantity'], i['available_quantity'], i['status']))
# 4. space
for s in data['space']:
cursor.execute("""
INSERT INTO space (id, company_id, name, start_at, end_at)
VALUES (%s, %s, %s, %s, %s)
""", (s['id'], s['company_id'], s['name'], s['start_at'], s['end_at']))
# 5. device
for d in data['device']:
cursor.execute("""
INSERT INTO device (id, company_id, role, created_at)
VALUES (%s, %s, %s, %s)
""", (d['id'], d['company_id'], d['role'], d['created_at']))
# 6. user
for u in data['user']:
cursor.execute("""
INSERT INTO user (id, company_id, name, phone, age, sex, created_at, updated_at)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
""", (u['id'], u['company_id'], u['name'], u['phone'], u['age'], u['sex'], u['created_at'], u['updated_at']))
# 7. usage_history
for uh in data['usage_history']:
cursor.execute("""
INSERT INTO usage_history (id, space_id, user_id, start_at, end_at)
VALUES (%s, %s, %s, %s, %s)
""", (uh['id'], uh['space_id'], uh['user_id'], uh['start_at'], uh['end_at']))
# 8. rental
for r in data['rental']:
cursor.execute("""
INSERT INTO rental (id, item_id, usage_id, quantity, returned_quantity, borrowed_at, returned_at)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""", (r['id'], r['item_id'], r['usage_id'], r['quantity'], r['returned_quantity'], r['borrowed_at'], r['returned_at']))
# 9. user_auth_code
for uac in data['user_auth_code']:
cursor.execute("""
INSERT INTO user_auth_code (id, auth_code, created_at, expired_at, phone)
VALUES (%s, %s, %s, %s, %s)
""", (uac['id'], uac['auth_code'], uac['created_at'], uac['expired_at'], uac['phone']))
# 최종 커밋
conn.commit()
cursor.close()
conn.close()
print("모든 mock 데이터가 DB에 삽입되었습니다.")