2023-09-20 16:50:35 +00:00
|
|
|
from re import M
|
2023-07-28 21:30:45 +00:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
import click
|
|
|
|
from flask import current_app, g
|
|
|
|
|
|
|
|
|
|
|
|
def get_db():
|
|
|
|
if 'db' not in g:
|
2023-09-20 16:50:35 +00:00
|
|
|
try:
|
|
|
|
g.db = sqlite3.connect(
|
|
|
|
current_app.config['DATABASE'],
|
|
|
|
detect_types=sqlite3.PARSE_DECLTYPES
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
init_db()
|
|
|
|
g.db = sqlite3.connect(
|
|
|
|
current_app.config['DATABASE'],
|
|
|
|
detect_types=sqlite3.PARSE_DECLTYPES
|
|
|
|
)
|
2023-07-28 21:30:45 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
def init_db():
|
|
|
|
db = get_db()
|
|
|
|
|
|
|
|
with current_app.open_resource('schema.sql') as f:
|
|
|
|
db.executescript(f.read().decode('utf8'))
|
|
|
|
|
|
|
|
|
|
|
|
@click.command('init-db')
|
|
|
|
def init_db_command():
|
|
|
|
"""Clear the existing data and create new tables."""
|
|
|
|
init_db()
|
|
|
|
click.echo('Initialized the database.')
|