-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.upload_db.py
More file actions
27 lines (20 loc) · 949 Bytes
/
6.upload_db.py
File metadata and controls
27 lines (20 loc) · 949 Bytes
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
from datetime import datetime
from json import load
from typing import Union
def sql_literal(val: Union[str, int, float, None]) -> str:
if val is None:
return 'NULL'
if isinstance(val, (int, float)):
return str(val)
if isinstance(val, str):
esc = val.replace("'", "''")
return f"'{esc}'"
raise TypeError(f"Unsupported type for SQL literal: {type(val)}")
with open('rooms_uploaded_image.json', 'r', encoding='utf-8') as data:
datas = load(data)
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
sql = ''
for data in datas:
sql += f"INSERT INTO room_entity(created_at, updated_at, place_name, unit_number, capacity, image) VALUES ({sql_literal(now)}, {sql_literal(now)}, {sql_literal(data['place_name'])}, {sql_literal(data['unit_number'])}, {sql_literal(data['capacity'])}, {sql_literal(data['image'])});\n"
with open("data.sql", 'w', encoding='utf-8') as file:
file.writelines(sql)