Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import sqlite3, os
  2.  
  3. from ac_data import app
  4.  
  5. from flask import g
  6.  
  7. app.config.update(dict(
  8. DATABASE=os.path.join(app.root_path, 'coindata.db'),
  9. SECRET_KEY='FuSNU+aa/OTCzmkyYCJsOvrUnacSalu5XTZ7tQp+gU7Ar0giKy',
  10. USERNAME='admin',
  11. PASSWORD='default'
  12. ))
  13.  
  14. app.config.from_envvar('AC_DATA_SETTINGS', silent=True)
  15.  
  16. def connect_db():
  17. rv = sqlite3.connect(app.config['DATABASE'])
  18. rv.row_factory = sqlite3.Row
  19. return rv
  20.  
  21. def init_db():
  22. db = get_db()
  23. with app.open_resource('schema.sql', mode='r') as f:
  24. db.cursor().executescript(f.read())
  25. db.commit()
  26.  
  27. @app.cli.command('initdb')
  28. def initdb_command():
  29. init_db()
  30. print('Initialized the database')
  31.  
  32. def get_db():
  33. if not hasattr(g, 'sqlite_db'):
  34. g.sqlite_db=connect_db()
  35. return g.sqlite_db
  36.  
  37. @app.teardown_appcontext
  38. def close_db(error):
  39. if hasattr(g, 'sqlite_db'):
  40. g.sqlite_db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement