Flask Tutorial – Flask SQLAlchemy with MySQL

In this Flask Tutorial we are going to learn about Flask SQLAlchemy with MySQL database, i will show you that how you can connect your Flask application to Mysql database using Flask SQLAlchemy, so first of all let’s talk about Flask.

 

 

What is Python Flask ?

In this Flask Tutorial, first let’s talk about Flask, so Python Flask is lightweight web framework that allows developers to build web applications in Python. Flask provides simple and flexible way to create web applications without the need for complex configuration or boilerplate code. it includes built in development server and supports different extensions and plugins for adding features such as authentication, database integration and templating engines.

  

 

Some of the key features of Flask include:

  1. Lightweight and flexible: Flask is designed to be simple and lightweight and it allows developers to build web applications quickly and easily.
  2. Routing and URL handling: Flask provides simple and flexible way to define routes and handle HTTP requests and responses.
  3. Templating engine: Flask supports several templating engines, which allow developers to create dynamic and interactive web pages.
  4. Built-in development server: Flask includes built in development server, which makes it easy to test and debug applications locally.
  5. Extensions and plugins: Flask supports different extensions and plugins and it provides additional features and functionality such as database integration, authentication and caching.

Flask is mostly used in web development for creating simple and scalable web applications, RESTful APIs and microservices. It is often used in combination with other Python libraries and tools, such as SQLAlchemy for database integration and Jinja2 for templating.

 

 

 

What is SQLAlchemy ?

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

 

 

 

What is Flask-SQLAlchemy ?

Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy with Flask by providing useful defaults and extra helpers that make it easier to accomplish common tasks. See the SQLAlchemy documentation to learn how to work with the ORM in depth. The following documentation is a brief overview of the most common tasks, as well as the features specific to Flask-SQLAlchemy.

 

 

 

Also if you are interested in Django, you can check this link Django Web Development Tutorials

 

 

 

How to Install Flask-SQLAlchemy?

You can simply install Flask-SQLAlchemy using pip command.

 

So now after installation you need to create database, we are using Mysql database. for this you need to download and install Wamp Server. create a database in the Wamp Server, iam going to give flaskcodeloop for the database name, but you can give the name according to your choice. right now we don’t have any table in our database, we will create this using Flask SQLAlchemy. We are going to use codes from our previous articles.  

 

 

 

How to Create SQLAlchemy with MySQL in Flask?

This is our app.py and we have added our SQLAlchmey and database configuration.

 

 

 

In the above code this is for SQLAlchemy.

 

 

 

 

And this is our database model class, it is just a simple model with three fields, id, username and password.

 

 

 

 

We have created our database model in our app.py, now we need to add this model in our Mysql Database, for this you need to open your Python terminal in Pycharm IDE. first we need to import our db from app, and after that we create our tables in the database.

 

 

 

 

If you check your flaskcodeloop database, you can see that we have our table with the data.

Flask Tutorial - Flask SQLAlchemy with MySQL
Flask Tutorial – Flask SQLAlchemy with MySQL

 

 

 

 

 

Now let’s add some data to our database. basically we are going to add just two UserInfo to our database table.

 

 

 

Check your database table, you will have two UserInfo data.

Flask Tutorial - Flask SQLAlchemy with Mysql
Flask Tutorial – Flask SQLAlchemy with Mysql9n

 

 

 

 

Selecting Data in Flask SQLAlchemy

Also you can select or retrieve the data from your Mysql Database, so now let’s open our python terminal in Pycharm IDE, and retrieve our data.

 

 

 

Updating Data in Flask SQLAlchemy

updating data is also an easy task with Flask SQLAlchemy, let’s update our first record in the database.

 

 

 

 

 

Now check your Mysql database, the first record is updated.

Flask SQLAlchemy Updating Data
Flask SQLAlchemy Updating Data

 

 

 

Deleting Data in Flask SQLAlchemy

Also you can delete data using this commands in Flask SQLAlchemy.

 

 

OK now we have done some basic operations in our database using Flask SQLAlchemy, it was just using python terminal, we will do that practically in the next articles. also these are the html files for the app.py file. 

 

 

This is our base.html, we have already talked about creating templates in Flask, you can read this article Introduction to Flask Templates. you need to just create templates folder in your working directory. 

 

 

templates/base.html

 

 

Our login.html with flash message.

 

 

templates/login.html

 

And this is our index.html file.

 

templates/index.html

 

 

 

This is forms.py file.

 

 

 

FAQs:

 

How to connect MySQL with Flask using SQLAlchemy?

For connecting MySQL with Flask using SQLAlchemy, follow these steps:

  • Install SQLAlchemy and Flask-MySQL package, you can use pip like this for the installation: pip install SQLAlchemy Flask-MySQL
  • In your Flask application, import SQLAlchemy and create an instance of it:

 

 

 

Can SQLAlchemy be used with MySQL?

Yes, SQLAlchemy can be used with MySQL. SQLAlchemy is a popular SQL toolkit and Object-Relational Mapping (ORM) library for Python, and it offers compatibility with different database systems including MySQL, PostgreSQL, SQLite and many more.

 

 

 

How do you use SQLAlchemy in Flask?

For using SQLAlchemy in Flask, follow these steps:

  • Install Flask-SQLAlchemy package, you can use pip for the installation: pip install Flask-SQLAlchemy
  • Import SQLAlchemy and create an instance of it in your Flask application:

 

 

How to create a database with Flask?

For creating a database with Flask, follow these steps:

  • Install Flask-SQLAlchemy package if not already installed: pip install Flask-SQLAlchemy
  • Import SQLAlchemy and create an instance of it in your Flask application:

  • Replace ‘sqlite:///your_database.db’ with the appropriate database URI for your chosen database system.
  • Define your models using SQLAlchemy’s ORM.
  • Run the following Flask command in your terminal to create the initial database:

This will create the necessary migration files and apply them to create the database structure.

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “Flask Tutorial – Flask SQLAlchemy with MySQL”

Leave a Comment

Share via
Copy link
Powered by Social Snap
×