21 lines
577 B
Python
21 lines
577 B
Python
import mysql.connector
|
|
from flask import current_app, g
|
|
|
|
def get_db():
|
|
if 'db' not in g:
|
|
g.db = mysql.connector.connect(
|
|
host=current_app.config['DB_HOST'],
|
|
port=current_app.config['DB_PORT'],
|
|
user=current_app.config['DB_USER'],
|
|
password=current_app.config['DB_PASSWORD'],
|
|
database=current_app.config['DB_NAME']
|
|
)
|
|
return g.db
|
|
|
|
def init_app(app):
|
|
@app.teardown_appcontext
|
|
def close_connection(exception):
|
|
db = g.pop('db', None)
|
|
if db is not None:
|
|
db.close()
|