-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
68 lines (60 loc) · 1.82 KB
/
Copy pathdatabase.py
File metadata and controls
68 lines (60 loc) · 1.82 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
# database.py
import sqlite3
import json
DB_PATH = 'service.db'
JSON_PATH = 'data/service_slots.json'
def init_db():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS service_slots (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT NOT NULL,
time TEXT NOT NULL,
booked INTEGER NOT NULL DEFAULT 0
)
''')
conn.commit()
# Optionally clear previous data
cursor.execute('DELETE FROM service_slots')
conn.commit()
# Seed from JSON
with open(JSON_PATH, 'r') as f:
slots = json.load(f)
for slot in slots:
cursor.execute(
'INSERT INTO service_slots (date, time, booked) VALUES (?, ?, ?)',
(slot['date'], slot['time'], int(slot['booked']))
)
conn.commit()
conn.close()
print("Database seeded from JSON.")
def seed_router_plans():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS router_plans (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
speed INTEGER NOT NULL,
price INTEGER NOT NULL
)
''')
conn.commit()
# Optionally clear previous data
cursor.execute('DELETE FROM router_plans')
conn.commit()
# Seed from JSON
with open(r'D:\void\data\router_plans.json', 'r') as f:
plans = json.load(f)
for plan in plans:
cursor.execute(
'INSERT INTO router_plans (name, speed, price) VALUES (?, ?, ?)',
(plan['name'], plan['speed'], plan['price'])
)
conn.commit()
conn.close()
print("Router plans seeded from JSON.")
if __name__ == "__main__":
#init_db()
seed_router_plans()