本文整理汇总了Python中flask.g._database方法的典型用法代码示例。如果您正苦于以下问题:Python g._database方法的具体用法?Python g._database怎么用?Python g._database使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask.g
的用法示例。
在下文中一共展示了g._database方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_db
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
cur = db.cursor()
cur.execute(
"""create table if not exists {} (
Telephone char(11) PRIMARY KEY NOT NULL,
Password char(20) NOT NULL,
Username char(20),
Sex char(1),
Location char(50))""".format(TABLE)
)
db.isolation_level = None
return db
示例2: close_db_connection
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def close_db_connection(exception):
db = getattr(g, "_database", None)
if db is not None:
db.close()
############################
######## Flask apps ########
############################
示例3: get_db
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_db():
db = getattr(g, '_database', None)
if db is None:
print("Creating new db connection {}".format(DATABASE))
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = make_dicts
return db
示例4: get_context_conn
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_context_conn():
"""
Flask needs a database connection per request context, and that
is registered to close in route.py
"""
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(db_name)
return db
示例5: get_db_con
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_db_con():
global pool
max_attempts = 10
con = getattr(g, '_database', None)
if con is None:
#Need to get a connection, use a try loop to handle pool depletions a bit better
#Otherwise psycopg2.pool throws exception and server returns 500 error to client
for attempt in range(1, max_attempts):
try:
con = g._database = pool.getconn()
if (attempt > 1):
log.debug("connection newly acquired from pool, attempt=%s" % attempt)
return con
except:
#On any errors, add exponentially increasing time delays.
#This seems to handle at least 30X the pool size in requests without hard errors.
e = sys.exc_info()[0]
log.error("exception during connection attempt=%s: %s" % (attempt, e))
if (attempt == max_attempts):
#give up!
raise
time.sleep(attempt**2)
else:
log.debug("connection reused from session variable.")
con.autocommit = True
return con
#Automatically return db connections
示例6: return_db_con
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def return_db_con(exception):
global pool
con = getattr(g, '_database', None)
if con is not None:
pool.putconn(con)
#log.debug("connection returned to pool.")
#format simple data for return as JSON
示例7: get_db
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
示例8: close_connection
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
示例9: get_db
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
with db:
try:
db.execute(
"CREATE TABLE numbers_table (name TEXT NOT NULL, number INTEGER NOT NULL)"
)
except Exception:
pass
return db
示例10: close_connection
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def close_connection(exception):
db = getattr(g, "_database", None)
if db is not None:
db.close()
示例11: get_db
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
with db:
try:
db.execute(
"CREATE TABLE numbers_table (name TEXT NOT NULL, number INTEGER NOT NULL)"
)
except:
pass
return db
示例12: get_cached_db
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import _database [as 别名]
def get_cached_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = get_db()
return db