35 lines
762 B
Python
35 lines
762 B
Python
import sqlite3
|
|
from flask import g
|
|
|
|
DATABASE = 'sqlite.db'
|
|
|
|
def get_db():
|
|
if 'db' not in g:
|
|
g.db = sqlite3.connect(DATABASE)
|
|
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(app):
|
|
with app.app_context():
|
|
db = get_db()
|
|
db.execute("PRAGMA journal_mode=WAL;")
|
|
db.execute("PRAGMA foreign_keys=ON;")
|
|
|
|
# create inital tables
|
|
db.execute("""
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE NOT NULL,
|
|
password TEXT NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
"""
|
|
)
|
|
|
|
|
|
db.commit() |