Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- from flask import Flask, g
- DATABASE = 'flask.db'
- DEBUG = True
- SECRET_KEY = 'my_pass'
- USERNAME = 'admin'
- PASSWORD = 'admin'
- app = Flask(__name__)
- app.config.from_object(__name__)
- def connect_db():
- rv = sqlite3.connect(app.config['DATABASE'])
- rv.row_factory = sqlite3.Row
- return rv
- def get_db():
- if not hasattr(g, 'sqlite_db'):
- g.sqlite_db = connect_db()
- return g.sqlite_db
- def init_db():
- with app.app_context():
- db = get_db()
- with app.open_resource('schema.sql',
- mode='r') as f:
- db.cursor().executescript(f.read())
- db.commit()
- @app.teardown_appcontext
- def close_db(error):
- if hasattr(g, 'sqlite_db'):
- g.sqlite_db.close()
- if __name__ == '__main__':
- init_db()
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement