Forums

MySql settings in a Flask app

Hello,

I just started web app developing a few months ago. I would like to use MySql for my web app, but I dont really know how to set up. Does anyone have a good tutorial or something for the configuration? I could really use an example code.

Thx in advance!

Cheers...

I think the best way to talk to the database in Flask is by using the Flask-SQLAlchemy plugin. This is a database abstraction layer that hides the details of which database you're using from your code, so you can use a sqlite or a MySQL database without needing to worry about which one you're using in most of your code, except at the specific point where you configure it to say where it should look for your data.

Flask-SQLAlchemy is installed by default on PythonAnywhere for Python 2.6 and 2.7 (installing it for Python 3 is pretty easy, but I see your web app is Python 2.7 anyway) and there's a tutorial on how to use it here.

Thank you for the reply. I checked your link, but I still don't really know how the Database Uri should look. I would like to use the mysql service provided by pythonanywhere. How should I modify this line: app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

I have the following details: MySQL Database host address: mysql.server Database username: InsulT

You'll need something like this:

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://InsulT:password@mysql.server/InsulT$default'

...where password is the password you set up on the "Databases" tab (not your web login password, and I recommend you make them different because having your web password in cleartext in your code would be a bad idea) and InsulT$default is the name of the database you create on the "Databases" tab (all MySQL databases on PythonAnywhere have names starting with your username, then a dollar sign).

[edit: removed extra whitespace from sample code]

Thank you. I really appreciate your help. This is what I was looking for. Have a nice day :)

No problem, glad to help!

how to call table in flask framework

That would depend on how you're connecting to the database. Check the docs of the library/ORM you're using.