-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathdb.py
More file actions
24 lines (17 loc) · 537 Bytes
/
db.py
File metadata and controls
24 lines (17 loc) · 537 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import sqlite3
from flask import g
DATABASE = 'db.db'
def get_db():
dbo = getattr(g, '_database', None)
if dbo is None:
dbo = g._database = sqlite3.connect(DATABASE)
return dbo
def init_db(app):
with app.app_context():
dbo = get_db()
with app.open_resource('schema.sql', mode='r') as f:
dbo.cursor().executescript(f.read())
dbo.commit()
def dict_factory(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))