Using raw SQL in Flask web applications to perform CRUD operations on database can be tedious. Instead, SQLAlchemy, a Python toolkit is a powerful OR Mapper that gives application developers the full power and flexibility of SQL. Flask-SQLAlchemy is the Flask extension that adds support for SQLAlchemy to your Flask application.
What is ORM (Object Relation Mapping)?
Flask: The Cheat Sheet Installation $ pip install Flask Hello World # myapp.py from flask import Flask app = Flask(name) @app.route(‘/’) def helloworld. In my last Python Flask article, I walked you through the building of a simple application to take in a Threat Stack webhook and archive the alert to AWS S3.In this post, I'll dive into Python exception handling and how to do it in a secure manner.
Most programming language platforms are object oriented. Data in RDBMS servers on the other hand is stored as tables. Object relation mapping is a technique of mapping object parameters to the underlying RDBMS table structure. An ORM API provides methods to perform CRUD operations without having to write raw SQL statements.
In this section, we are going to study the ORM techniques of Flask-SQLAlchemy and build a small web application.
Step 1 − Install Flask-SQLAlchemy extension.
Step 2 − You need to import SQLAlchemy class from this module.
Step 3 − Now create a Flask application object and set URI for the database to be used.
Step 4 − Then create an object of SQLAlchemy class with application object as the parameter. This object contains helper functions for ORM operations. It also provides a parent Model class using which user defined models are declared. In the snippet below, a students model is created.
Step 5 − To create / use database mentioned in URI, run the create_all() method.
The Session object of SQLAlchemy manages all persistence operations of ORM object.
The following session methods perform CRUD operations −
- db.session.add(model object) − inserts a record into mapped table
- db.session.delete(model object) − deletes record from table
- model.query.all() − retrieves all records from table (corresponding to SELECT query).
You can apply a filter to the retrieved record set by using the filter attribute. For instance, in order to retrieve records with city = ’Hyderabad’ in students table, use following statement −
With this much of background, now we shall provide view functions for our application to add a student data.
The entry point of the application is show_all() function bound to ‘/’ URL. The Record set of students table is sent as parameter to the HTML template. The Server side code in the template renders the records in HTML table form.
The HTML script of the template (‘show_all.html’) is like this −
The above page contains a hyperlink to ‘/new’ URL mapping new() function. When clicked, it opens a Student Information form. The data is posted to the same URL in POST method.
new.html
When the http method is detected as POST, the form data is added in the students table and the application returns to homepage showing the added data.
Given below is the complete code of application (app.py).
Run the script from Python shell and enter http://localhost:5000/ in the browser.
Click the ‘Add Student’ link to open Student information form.
Fill the form and submit. The home page reappears with the submitted data.
We can see the output as shown below.