Configuring MySQL database inside Flask factory function without SQLAlchemy
Question:
I am new to Flask and trying to build a simple application with a MySQL database. However, I prefer not to use SQLAlchemy. Instead, I would like to use mysql.connector
and direct SQL statements to SELECT, INSERT, UPDATE or DELETE from the database.
I have a question: is it mandatory to define the MySQL database connection/parameters inside the factory function? The Flask tutorial shows a SQLite database being defined inside the factory function, create_app()
.
Sample tutorial code:
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
@app.route('/hello')
def hello():
return 'Hello, World!'
return app
Answers:
Okay, so I found the solution myself. It is not mandatory to define the MySQL database connection inside the factory function and it is also not necessary to use SQLALchemy. I used mysql-connector-python
inside a separate database connection module and it worked fine.
However, if you want to use db connection pooling, it is necessary to create the connection pool inside the factory function so that it is initialised at start up.
I am new to Flask and trying to build a simple application with a MySQL database. However, I prefer not to use SQLAlchemy. Instead, I would like to use mysql.connector
and direct SQL statements to SELECT, INSERT, UPDATE or DELETE from the database.
I have a question: is it mandatory to define the MySQL database connection/parameters inside the factory function? The Flask tutorial shows a SQLite database being defined inside the factory function, create_app()
.
Sample tutorial code:
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
@app.route('/hello')
def hello():
return 'Hello, World!'
return app
Okay, so I found the solution myself. It is not mandatory to define the MySQL database connection inside the factory function and it is also not necessary to use SQLALchemy. I used mysql-connector-python
inside a separate database connection module and it worked fine.
However, if you want to use db connection pooling, it is necessary to create the connection pool inside the factory function so that it is initialised at start up.