diff --git a/flaskr/db.py b/flaskr/db.py new file mode 100644 index 0000000..bb80966 --- /dev/null +++ b/flaskr/db.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import sqlite3 +import click +from flask import current_app, g + + +def get_db(): + if 'db' not in g: + g.db = sqlite3.connect( + current_app.config['DATABASE'], + detect_types=sqlite3.PARSE_DECLTYPES + ) + g.db.row_factory = sqlite3.Row + + return g.db + + +def close_db(e=None): + db = g.pop('db', None) + + if db is not None: + db.close()