
Creating a Database with Flask SQLAlchemy for Software Engineering Projects
"Learn how to set up and configure a database using Flask SQLAlchemy in the context of CS373 Software Engineering. This comprehensive guide covers requirements installation, creating models, and connecting the database. Follow step-by-step instructions to efficiently establish your database for your software projects."
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. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
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.
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
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 Book(db.Model): __tablename__ = 'book' id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(80), nullable = False) details = db.relationship('BookDetails', backref = 'book', uselist = False)
Creating a database with SQLAlchmey Configuration code: import all necessary modules Creating models class BookDetails(db.Model): __tablename__ = 'bookdetails' id = db.Column(db.Integer, primary_key = True) detail = db.Column(db.String(255)) book_id = db.Column(db.Integer, db.ForeignKey('book.id'))
Populate Database Import from models Create a publisher Create a book Query examples
Populate Database See create_db.py (1-1)