-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
23 lines (17 loc) · 722 Bytes
/
db.py
File metadata and controls
23 lines (17 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sqlite3
import pathlib
def dict_factory(cursor, row):
"""Convert database row objects to a dictionary keyed on column name.
This is useful for building dictionaries which are then used to render a
template. Note that this would be inefficient for large queries.
"""
return {col[0]: row[idx] for idx, col in enumerate(cursor.description)}
def get_db():
""" create a database connection to a SQLite database """
ROOT = pathlib.Path(__file__).resolve().parent
DATABASE_FILENAME = ROOT/'var'/'calpal.sqlite3'
# print(DATABASE_FILENAME)
db = sqlite3.connect(str(DATABASE_FILENAME))
db.row_factory = dict_factory
db.execute("PRAGMA foreign_keys = ON")
return db