-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
85 lines (71 loc) · 2.48 KB
/
database.py
File metadata and controls
85 lines (71 loc) · 2.48 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
import sqlite3
import os
# Get the absolute path of transit.db
BASE_DIR = os.path.dirname(
os.path.abspath(__file__)
) # Gets the directory of the script
DB_PATH = os.path.join(
BASE_DIR, "..", "transit.db"
) # Moves up one level to store db in /data/
def get_db_connection():
"""Establish and return an SQLite connection."""
return sqlite3.connect(DB_PATH)
def insert_library(location, address, latitude, longitude):
"""Insert a library into the database."""
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"""
INSERT OR IGNORE INTO libraries (location, address, latitude, longitude)
VALUES (?, ?, ?, ?)
""",
(location, address, latitude, longitude),
)
conn.commit()
conn.close()
def insert_printer(location, description, labels, latitude, longitude):
"""Insert a printer into the database."""
conn = get_db_connection()
cursor = conn.cursor()
# We remove the "OR IGNORE" because we acknoledge that several printers may have the same location and description (i.e., same building and room), so we rely on the unique printer_id to identify the printer
cursor.execute(
"""
INSERT INTO printers (location, description, latitude, longitude)
VALUES (?, ?, ?, ?)
""",
(location, description, latitude, longitude),
)
# To get the printer_id, we do NOT rely on the location/description/coordinates, but rather on the printer_id that was just inserted (lastrowid), as several printers may have the same location and description (i.e., same building and room)
printer_id = cursor.lastrowid
# Insert labels into the labels table and get their IDs
label_ids = []
for label in labels:
cursor.execute(
"""
INSERT OR IGNORE INTO labels (label)
VALUES (?)
""",
(label,),
)
cursor.execute(
"""
SELECT id FROM labels WHERE label = ?
""",
(label,),
)
result = cursor.fetchone()
if result is None:
raise ValueError(f"Failed to find label: {label}")
label_id = result[0]
label_ids.append(label_id)
# Insert into junction table
for label_id in label_ids:
cursor.execute(
"""
INSERT OR IGNORE INTO printer_labels (printer_id, label_id)
VALUES (?, ?)
""",
(printer_id, label_id),
)
conn.commit()
conn.close()