def __init__(self, app):
"""Constructor."""
self.app = app
@app.teardown_appcontext
def close_connection(exception):
"""Close database connection when finished handling request."""
db = getattr(g, '_database', None)
if db is not None:
db.close()
python类_database()的实例源码
def get_db(self):
"""Return the app global db connection or create one."""
db = getattr(g, '_database', None)
if db is None:
db = g._database = self.connect_to_db()
db.row_factory = sqlite3.Row
return db
def get_db():
db = getattr(g, '_database', None)
if db is None:
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
db = g._database = sqlite3.connect('craftbeerpi.db')
db.row_factory = dict_factory
return db
def get_db():
"""
From the Flask Websitei, probs best practice to connect to the databse this way
"""
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(dbpath)
def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))
db.row_factory = make_dicts
return db
def get_db():
db = getattr(g, '_database', None)
if db is None:
g._database = Database()
return g._database
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.disconnect()
def set_db():
dbx = getattr(g, '_database', None)
if dbx is None:
dbx = g._database = db
sqlite_basic_flask.py 文件源码
项目:zajecia_python_mini_edycja4
作者: daftcode
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = sqlite3.connect(DATABASE)
g._database = db
return db
# zgapione z http://flask.pocoo.org/docs/0.12/patterns/sqlite3/
sqlite_basic_flask.py 文件源码
项目:zajecia_python_mini_edycja4
作者: daftcode
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
#@app.route('/<new_user>')
#def insert_new_user(new_user):
# db = get_db()
# c = db.cursor()
# c.execute("INSERT INTO users VALUES ('{}', 3)".format(new_user))
# db.commit()
# return 'inserted {}'.format(new_user)
def get_db():
db = getattr(g,'_database',None)
if db is None:
db = g._database = sqlite3.connect(DATABASE_URL)
db.row_factory = make_dicts
return db
# ??SQL?????????
def close_connection(exeption):
db = getattr(g,'_database',None)
if db is not None:
db.close()
def get_db():
db = getattr(g, '_database',None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
def close_connection(exception):
db = getattr(g,'_database', None)
if db is not None:
db.close()
def get_db():
# check if _database attribute exist
db = getattr(g, '_database', None)
if db is None:
# if doesnt, establish a new connection and save _database attribute
# this prevents multiple instances to the database
db = g._database = sql.connect(DATABASE)
db.row_factory = sql.Row
return db
# @function for querying/retrieving database
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_db()
return db
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_database()
return db
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
#############
# HOME PAGE #
#############