Understanding Flask-SQLAlchemy for Efficient Database Management
Flask-SQLAlchemy, a powerful tool for database management in Python, offers a seamless way to interact with databases using object-oriented concepts. By integrating SQLAlchemy's ORM capabilities with Flask, developers can simplify database operations and enhance productivity. This article provides an overview of setting up and configuring databases with Flask-SQLAlchemy, illustrating the benefits of using ORM for database interactions within Python applications.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Flask SQLAlchemy Populate CS373 Software Engineering
Requirements Install Postgresql Install SQLAlchmey - pip install Flask-SQLAlchemy - pip install psycopg2 Create models.py
Outline SQLAlchemy Flask-SQLAlchmey Setup and configure the database
SQLAlchmey - Motivation Interacting with a database can be done using SQL syntax Using Flask, one can still use raw SQL Sending a query to the database will be as a strings But, python complier has no way of checking if there is a typo or reference a table that does not exist In Python (flask), every other data structure we use is some type of object. So, why not treat our database queries, tables, and rows as objects as well.
SQLAlchmey - Motivation Developer have created Object Relational Mapping (ORM) to handle such a situation. You can think of ORM as a translator, converting code from one form to another. One sends off code written in python The code is transformed by the ORM into SQL and sent off to the database The ORM also gets results from an SQL statement and allows us to use it as an object from within the Python code. SQLAlchemy is an open source ORM for Python with many features. https://www.sqlalchemy.org/library.html#tutorials
Flask-SQLAlchmey Flask extension to add support for SQLAlchmey to flask apps Facilitate connecting Python code to Database Provide a mapping between the Python classes with tables in the database
Creating a database with SQLAlchmey Configuration code: import all necessary modules - At the beginning of the file Import all modules needed Create a Flask object, have it pointing to the database, create an SQLAlchemy object and bind it to the Flask app - Creating models Class code: used to represent our data in Python Table: used to represent the specific table in the database Mapper: used to connect the columns of the table to the class that represents it - At the end of the file Create (or connect) the database and adds tables and columns
Creating a database with SQLAlchmey Configuration code: import all necessary modules - At the beginning of the file Import all modules needed from flask import Flask from flask_sqlalchemy import SQLAlchemy import os Create a Flask object, have it pointing to the database, create an SQLAlchemy object and bind it to the Flask app
Creating a database with SQLAlchmey Configuration code: import all necessary modules - At the beginning of the file Import all modules needed Create a Flask object, have it pointing to the database, create an SQLAlchemy object and bind it to the Flask app app = Flask(__name__) app.app_context().push() app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:asd123@localhost:5432/bookdb' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True # to suppress a warning message db = SQLAlchemy(app)
Creating a database with SQLAlchmey Configuration code: import all necessary modules - At the beginning of the file Import all modules needed Create a Flask object, have it pointing to the database, create an SQLAlchemy object and bind it to the Flask app - Creating models (will do it next) - At the end of the file Create (or connect) the database and adds tables and columns db.create_all()
Creating a database with SQLAlchmey Configuration code: import all necessary modules Creating models - Class code: used to represent our data in Python - Table: used to represent the specific table in the database - Mapper: used to connect the columns of the table to the class that represents it class Publisher(db.Model): __tablename__ = 'publisher' name = db.Column(db.String(80), nullable = False) id = db.Column(db.Integer, primary_key = True)) books = db.relationship('Book', backref = 'publisher')
Creating a database with SQLAlchmey Configuration code: import all necessary modules Creating models class Book(db.Model): __tablename__ = 'book' title = db.Column(db.String(80), nullable = False) id = db.Column(db.Integer, primary_key = True) description = db.Column(db.String(250)) price = db.Column(db.String(8)) # This property refer to the "id" property in the "publisher" tabel publisher_id = db.Column(db.Integer, db.ForeignKey('publisher.id'))
Populate Database Import from models Create a publisher Create a book Query examples
Populate Database See create_db.py (1-M)