make database sqlite compatible.
This commit is contained in:
23
db.py
23
db.py
@@ -5,10 +5,15 @@ from flask import current_app, g
|
||||
from flask.cli import with_appcontext
|
||||
|
||||
|
||||
def get_db():
|
||||
def get_db(mod_path=None):
|
||||
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(
|
||||
current_app.config["DATABASE_URI"],
|
||||
path,
|
||||
detect_types=sqlite3.PARSE_DECLTYPES
|
||||
)
|
||||
g.db.row_factory = sqlite3.Row
|
||||
@@ -21,18 +26,18 @@ def close_db(e=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')
|
||||
@with_appcontext
|
||||
def init_db_command():
|
||||
"""Clear the existing data and create new tables."""
|
||||
init_db()
|
||||
click.echo('Initialized the database.')
|
||||
path = "./users.db"
|
||||
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):
|
||||
app.teardown_appcontext(close_db)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
DROP TABLE IF EXISTS user;
|
||||
|
||||
PRAGMA journal_mode=wal;
|
||||
CREATE TABLE user (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL,
|
||||
|
||||
Reference in New Issue
Block a user