Flask SQLAlchemy Tutorial for Database Management

In this Flask Tutorial we want to learn about Flask SQLAlchemy Tutorial for Database Management, so Flask is popular Python web framework, it means that using Flask you can build Python Web Applications easliy. one of the key features of Flask is its ability to integrate with different third party libraries like SQLAlchemy, so SQLAlchemy is powerful Object Relational Mapping or ORM library for database management.



In this Flask Tutorial we want to learn how to use Flask and SQLAlchemy to manage databases in Flask web application. We will cover the following topics:



Creating Flask Application

Before that we talk about database management in Flask, we need to create our simple boilerplate code for Flask applications, make sure that you have already installed Python and Flask. if not, you can follow the Flask documentation to install Flask.



First of all we need to create a new directory for our Flask project and navigate to it. and then we need to create a new Python file called app.py. inside app.py add this code:

This code creates a new Flask application and defines a route for the root URL. when the user navigates to the root URL hello() function will be called and return a simple greeting.




If you got, this will be the result.

Flask SQLAlchemy Tutorial for Database Management
Flask SQLAlchemy Tutorial for Database Management




Setup Flask Database Connection

Now that we have our Flask application, we can configure our database connection. in this tutorial we want to use SQLite as our database. so SQLite is lightweight and file based database system, it is easy to set up and use then MySQL.



First of all we need to install the Flask-SQLAlchemy extension because it provides a simple interface for using SQLAlchemy in Flask. you can install it by running pip install flask-sqlalchemy in your terminal.



After that we need to configure our database connection. add this code in your app.py, this code configures our Flask application to use SQLite and creates a new SQLAlchemy object called db. we also set SQLALCHEMY_TRACK_MODIFICATIONS to False to turn off SQLAlchemy modification tracking system. this is recommended optimization for performance reasons, but may not be necessary for small applications.




Flask Models and Database Table

Now that we have our database connection, we can define our database models. in SQLAlchemy a model is a Python class that represents a database table. each attribute of the class represents a column in the table.


For this tutorial, we want to create a simple User model with id, name and email attributes. add this code to app.py:



Until this point, this will be our complete code for app.py.





But right now we don’t have the table, we need to open Flask Shell in the terminal like this.



And after that we need to write these commands in terminal to create our database and table.




If we open the database in SQLiteStudio, this will be the result

Flask SQLAlchemy Tutorial for Database Management
Flask SQLAlchemy Tutorial for Database Management




Flask CRUD Operations on the Database

Now that we have defined our model and created our database tables, we can start performing Flask CRUD operations on the database. SQLAlchemy provides a powerful and easy API for working with databases, and Flask-SQLAlchemy makes it even easier to use.


First you need to open flask shell, we want to do our operations in Flask shell terminal, to create a new user, we can simply create a new User object and add it to the database session:



These commands create a new user and if you check your database. this will be the result

Flask SQLAlchemy Tutorial
Flask SQLAlchemy Tutorial



To retrieve all users from the database, we can use query.all() method:



This will be the result

Flask SQLAlchemy
Flask SQLAlchemy



For updating an existing user, we can retrieve the user from the database, update its attributes and commit the changes, this code retrieves the user with the name “GeeksCoders” from the database using filter_by() and first(), and after that updates its email attribute and commits the changes to the database.



Also we can delete a user from the database, for that we can simply delete the User object and commit the changes:



Learn More

Leave a Comment