Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import sqlite3
  2. from flask import Flask, g
  3.  
  4. DATABASE = 'flask.db'
  5. DEBUG = True
  6. SECRET_KEY = 'my_pass'
  7. USERNAME = 'admin'
  8. PASSWORD = 'admin'
  9.  
  10. app = Flask(__name__)
  11. app.config.from_object(__name__)
  12.  
  13. def connect_db():
  14. rv = sqlite3.connect(app.config['DATABASE'])
  15. rv.row_factory = sqlite3.Row
  16. return rv
  17.  
  18. def get_db():
  19. if not hasattr(g, 'sqlite_db'):
  20. g.sqlite_db = connect_db()
  21. return g.sqlite_db
  22.  
  23. def init_db():
  24. with app.app_context():
  25. db = get_db()
  26. with app.open_resource('schema.sql',
  27. mode='r') as f:
  28. db.cursor().executescript(f.read())
  29. db.commit()
  30.  
  31. @app.teardown_appcontext
  32. def close_db(error):
  33. if hasattr(g, 'sqlite_db'):
  34. g.sqlite_db.close()
  35.  
  36.  
  37. if __name__ == '__main__':
  38. init_db()
  39. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement