本文整理匯總了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