You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
705 B
25 lines
705 B
from flask import current_app, g |
|
import pymysql |
|
from pymysql.cursors import DictCursor |
|
|
|
def get_db(): |
|
if "db" not in g: |
|
g.db = pymysql.connect( |
|
host=current_app.config["MARIADB_HOST"], |
|
port=current_app.config["MARIADB_PORT"], |
|
user=current_app.config["MARIADB_USER"], |
|
password=current_app.config["MARIADB_PASSWORD"], |
|
database=current_app.config["MARIADB_DB"], |
|
cursorclass=DictCursor, |
|
autocommit=False, |
|
charset="utf8mb4", |
|
) |
|
return g.db |
|
|
|
def close_db(_e=None): |
|
db = g.pop("db", None) |
|
if db is not None: |
|
db.close() |
|
|
|
def init_app(app): |
|
app.teardown_appcontext(close_db)
|
|
|