Flask-MySQL¶
Flask-MySQL is a Flask extension that allows you to access a MySQL database.
You can report bugs and discuss features on the issues page.
Installation¶
Use pip to install flask-mysql
:
pip install flask-mysql
Configuration¶
To configure access to your MySQL database server by using these settings :
|
default is ‘localhost’ |
|
default is 3306 |
|
default is None |
|
default is None |
|
default is None |
|
default is ‘utf-8’ |
Usage¶
Initialize the extension :
from flaskext.mysql import MySQL
mysql = MySQL()
mysql.init_app(app)
Obtain a cursor :
cursor = mysql.get_db().cursor()
Multiple connection example:
from flaskext.mysql import MySQL
my_database = MySQL(app, prefix="my_database", host="hostname, user="username", password="password", db="my_database", autocommit=True)
our_database = MySQL(app, prefix="our_database", host="hostname, user="username", password="password", db="our_database", autocommit=True, cursorclass=pymysql.cursors.DictCursor)
@app.route('/')
@app.route('/index')
def hello():
my_cursor = my_database.get_db().cursor()
#...
our_cursor = our_database.get_db().cursor()
#...