@app.teardown_appcontext
def close_database(exception):
 top = _app_ctx_stack.top
 if hasattr(top, 'sqlite_db'):
  top.sqlite_db.close()

@ app . teardown_appcontext def close_database ( exception ) : top = _app_ctx_stack . top if hasattr ( top , 'sqlite_db' ) : top . sqlite_db . close ( )
closes the database again at the end of the request .