Flask – SQLAlchemy

Using raw SQL in Flask web applications to perform CRUD operations on database can be tedious. Rather, SQLAlchemy, a Python toolbox is an powerful OR Mapper that gives application designers the full power and flexibility of SQL. Carafe SQLAlchemy is the Flask expansion that adds support for SQLAlchemy to your Flask application.

What is ORM (Object Relation Mapping)?

Most programming language stages are item arranged. Information in RDBMS servers then again is put away as tables. Object relation mapping is a technique of mapping object parameters to the hidden RDBMS table structure. An ORM API gives strategies to perform CRUD activities without composing write SQL statements.

In this section, we are going to think about the ORM techniques of Flask-SQLAlchemy and manufacture a little web application.

Step 1 − Install Flask-SQLAlchemy extension.

pip install flask-sqlalchemy
Step 2 − You need to import SQLAlchemy class from this module.

from flask_sqlalchemy import SQLAlchemy

Step 3 − Now create a Flask application object and set URI for the database to be used.

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'

Step 4 − At that point make an object of SQLAlchemy class with application object as the parameter. This object contains partner capacities for ORM operations. It likewise gives a parent Model class utilizing which client characterized models are proclaimed. In the piece beneath, a students model is made.