Difference between revisions of "Connecting MySQL to Flask Web App"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Adding MySQL Support= Download the following [https://studentthomrothac-my.sharepoint.com/:u:/g/personal/wayne_jones_thomroth_ac_uk/EXzDZfZyWLVCgnGiPAVXlb4B8v9UUGUOwcLB1GHQcV...")
(No difference)

Revision as of 19:44, 9 April 2019

Adding MySQL Support

Download the following zip file and extract it into your site-packages folder. If you are using your own machine installing flask-mysql an pymysql will do the same thing.

We need to run the USBWebserver, in College this must be on your compiled storage folder (download it HERE). When you run the executable you should get 2 firewall messages, click allow. If you get no firewall messages on the first run, then your system is preventing it from running:

Webserver.png

This shows USBWebserver running and fully active. Now click PHPmyAdmin button to open PHPmyAdmin:

Now click on database and enter a name to create a new database:

You should now be able to add the following code to import MySQL and connect to a database:

from flaskext.mysql import MySQL

mysql = MySQL()
 
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'usbw'
app.config['MYSQL_DATABASE_DB'] = 'Test'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

conn = mysql.connect()