-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
74 lines (56 loc) · 2.11 KB
/
db.py
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
import sqlite3
import sys
from tabulate import tabulate
from termcolor import colored
def connect_db(DB_FILE_NAME):
"""
Open a connection to a SQLite database. If a sqlite3.Error is raised, exits the program.
:param DB_FILE_NAME: The path to the database file to be opened
:type DB_FILE_NAME: string
"""
try:
db = sqlite3.connect(DB_FILE_NAME)
return db
except sqlite3.Error as e:
sys.exit(f"Error connecting to the database: {e}")
def init_db(DB_FILE_NAME, SCHEMA_SQL):
"""
Initialize the database.
:param str DB_FILE_NAME: Path to the database file to be opened
:param str SCHEMA_SQL: Path to the schema.sql file to be used for initialization
"""
confirm = input("Are you sure you want to initialize the database? (y/n) ").upper()
while confirm not in ["Y", "N", "YES", "NO"]:
confirm = input("Are you sure you want to initialize the database? (y/n) ").upper()
if confirm == "Y" or confirm == "YES":
with connect_db(DB_FILE_NAME) as db:
cur = db.cursor()
with open(SCHEMA_SQL) as f:
cur.executescript(f.read())
print(colored("Database initialized.", "black", "on_white"))
else:
print(colored("Canceled database initialization.", "black", "on_white"))
def load_db(DB_FILE_NAME):
"""
Load the database into memory as a dict object.
:param str DB_FILE_NAME: Path to the database file to be loaded
"""
with connect_db(DB_FILE_NAME) as db:
# change row_factory
db.row_factory = sqlite3.Row
cur = db.cursor()
# fetch data
cur.execute("SELECT * FROM inv")
dict = {}
for r in cur.fetchall():
dict.update({r["item"]: r["quantity"]})
return dict
def tabulate_db(inv):
"""
Return plain-text table of inventory.
:param str inv: dictionary of database
"""
inv_list = []
for key in sorted(inv.keys()):
inv_list.append([key, inv[key]])
return tabulate(inv_list, tablefmt="grid", headers=["Item", "Quantity"])