-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
42 lines (34 loc) · 1.08 KB
/
models.py
File metadata and controls
42 lines (34 loc) · 1.08 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
import sqlite3
from flask import g
from config import Config
import os
#Les fonctions pour gérer la db
"""Ouverture de la DB
Retourne un objet pour accéder à la DB"""
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(Config.DATABASE)
return g.db
"""Ferme la DB"""
def close_db(exception):
db = g.pop('db', None)
if db is not None:
db.close()
def delete_db():
"""Supprime la DB si elle existe"""
db_path = Config.DATABASE
if os.path.exists(db_path):
os.remove(db_path)
def init_db():
db=get_db()
schema_path = os.path.join(os.path.dirname(__file__), 'db', 'schema.sql')
schema_path2 = os.path.join(os.path.dirname(__file__), 'db', 'basic_insertion.sql')
with open(schema_path, 'r') as f:
db.executescript(f.read())
with open(schema_path2, 'r') as f:
db.executescript(f.read())
def insert_test_data():
db = get_db()
test_data_path = os.path.join(os.path.dirname(__file__), 'db', 'test_sample.sql')
with open(test_data_path, 'r') as f:
db.executescript(f.read())