Python flask.g 模块,_database() 实例源码

我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用flask.g._database()

项目:globus-sample-data-portal    作者:globus    | 项目源码 | 文件源码
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()
项目:globus-sample-data-portal    作者:globus    | 项目源码 | 文件源码
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
项目:craftbeerpi3    作者:Manuel83    | 项目源码 | 文件源码
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
项目:SlackRU    作者:HackRU    | 项目源码 | 文件源码
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
项目:CMS    作者:Vinlagrenouille    | 项目源码 | 文件源码
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        g._database = Database()
    return g._database
项目:CMS    作者:Vinlagrenouille    | 项目源码 | 文件源码
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.disconnect()
项目:secret-weapon    作者:junorouse    | 项目源码 | 文件源码
def set_db():
    dbx = getattr(g, '_database', None)
    if dbx is None:
        dbx = g._database = db
项目:zajecia_python_mini_edycja4    作者:daftcode    | 项目源码 | 文件源码
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/
项目:zajecia_python_mini_edycja4    作者:daftcode    | 项目源码 | 文件源码
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)
项目:demo_project_bms    作者:solheng    | 项目源码 | 文件源码
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?????????
项目:demo_project_bms    作者:solheng    | 项目源码 | 文件源码
def close_connection(exeption):
    db = getattr(g,'_database',None)
    if db is not None:
        db.close()
项目:baroness    作者:ulrichknecht    | 项目源码 | 文件源码
def get_db():
    db = getattr(g, '_database',None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db
项目:baroness    作者:ulrichknecht    | 项目源码 | 文件源码
def close_connection(exception):
    db = getattr(g,'_database', None)
    if db is not None:
        db.close()
项目:PSP0201    作者:prevwong    | 项目源码 | 文件源码
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
项目:PSP0201    作者:prevwong    | 项目源码 | 文件源码
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()
项目:hackathon    作者:vertica    | 项目源码 | 文件源码
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = connect_to_db()
    return db
项目:hackathon    作者:vertica    | 项目源码 | 文件源码
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()
项目:cloud-gateway    作者:BU-NU-CLOUD-SP16    | 项目源码 | 文件源码
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = connect_to_database()
    return db
项目:cloud-gateway    作者:BU-NU-CLOUD-SP16    | 项目源码 | 文件源码
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

#############
# HOME PAGE #
#############