-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
49 lines (38 loc) · 1.59 KB
/
database.py
File metadata and controls
49 lines (38 loc) · 1.59 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
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, latitude, longitude):
"""Insert a printer into the database."""
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT OR IGNORE INTO printers (location, description, latitude, longitude)
VALUES (?, ?, ?, ?)
''', (location, description, latitude, longitude))
conn.commit()
conn.close()
def insert_restaurant(name, category, address, latitude, longitude, image_url, web_url):
"""Insert a restaurant into the database."""
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT OR IGNORE INTO restaurants (name, category, address, latitude, longitude, image_url, web_url)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (name, category, address, latitude, longitude, image_url, web_url))
conn.commit()
conn.close()