make database sqlite compatible.

This commit is contained in:
pstruebi
2022-06-17 11:23:33 +02:00
parent 7115f93810
commit 8dafb470b7
2 changed files with 15 additions and 10 deletions

23
db.py
View File

@@ -5,10 +5,15 @@ from flask import current_app, g
from flask.cli import with_appcontext from flask.cli import with_appcontext
def get_db(): def get_db(mod_path=None):
if 'db' not in g: if 'db' not in g:
# provide modified path if is not None
if mod_path is None:
path=current_app.config["DATABASE_URI"]
else:
path=mod_path
g.db = sqlite3.connect( g.db = sqlite3.connect(
current_app.config["DATABASE_URI"], path,
detect_types=sqlite3.PARSE_DECLTYPES detect_types=sqlite3.PARSE_DECLTYPES
) )
g.db.row_factory = sqlite3.Row g.db.row_factory = sqlite3.Row
@@ -21,18 +26,18 @@ def close_db(e=None):
if db is not None: if db is not None:
db.close() 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') @click.command('init-db')
@with_appcontext @with_appcontext
def init_db_command(): def init_db_command():
"""Clear the existing data and create new tables.""" """Clear the existing data and create new tables."""
init_db() path = "./users.db"
click.echo('Initialized the database.') db = get_db(mod_path="users.db")
with current_app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
click.echo('Initialized the database at %s. Copy it to the according directory.'% path)
def init_app(app): def init_app(app):
app.teardown_appcontext(close_db) app.teardown_appcontext(close_db)

View File

@@ -1,5 +1,5 @@
DROP TABLE IF EXISTS user; DROP TABLE IF EXISTS user;
PRAGMA journal_mode=wal;
CREATE TABLE user ( CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL, username TEXT NOT NULL,