Flask Mysql database connection


How to connect a MySQL database with you Flask python app. MySQL is the most commonly used database today, here is the code

app.config['DATABASE_FILE'] = 'uchat'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://scott:tiger@localhost/' + app.config['DATABASE_FILE']
app.config['SECRET_KEY'] = '123456790'
app.config['SQLALCHEMY_ECHO'] = True

With MySQL driver called pymysql which is available in Python Package Index  Library, you can connect even local database for development purpose.  To install this package use  pip

Run the following command from your command prompt

pip install pymysql

and pip will download and install necessary packages.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.