Database and SQLAlchemy

In this blog we will explore using programs with data, focused on Databases. We will use SQLite Database to learn more about using Programs with Data.

  • College Board talks about ideas like

    • Program Usage. "iterative and interactive way when processing information"
    • Managing Data. "classifying data are part of the process in using programs", "data files in a Table"
    • Insight "insight and knowledge can be obtained from ... digitally represented information"
    • Filter systems. 'tools for finding information and recognizing patterns"
    • Application. "the preserve has two databases", "an employee wants to count the number of book"
  • PBL, Databases, Iterative/OOP

    • Iterative. Refers to a sequence of instructions or code being repeated until a specific end result is achieved
    • OOP. A computer programming model that organizes software design around data, or objects, rather than functions and logic
    • SQL. Structured Query Language, abbreviated as SQL, is a language used in programming, managing, and structuring data

Imports and Flask Objects

Defines and key object creations

  • Comment on where you have observed these working?
  1. Flask app object Ive seen this working in the CPT projects in flask
  2. SQLAlchemy object This is in the model and the API
"""
These imports define the key objects
"""

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

"""
These object and definitions are used throughout the Jupyter Notebook.
"""
database = 'sqlite:///sqlite.db'  # path and filename of database
# Setup of key Flask object (app)
app = Flask(__name__)
# Setup SQLAlchemy object and properties for the database (db)
database = 'sqlite:///sqlite.db' # path and filename of database
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = database
app.config['SECRET_KEY'] = 'SECRET_KEY'
db = SQLAlchemy()


# This belongs in place where it runs once per project
db.init_app(app)

Model Definition

Define columns, initialization, and CRUD methods for users table in sqlite.db

  • Comment on these items in the class
  • class User purpose
    • The purpose of a "User" class depends on the specific software application or system that it is being used in. In general, a User class represents an individual who is interacting with the system, and it typically includes attributes such as the user's name, email, password, and other information that is relevant to the application.
  • db.Model inheritance
    • In Flask and SQLAlchemy, you can create a hierarchy of classes using inheritance to model related database tables. When you define a model in SQLAlchemy, you can create a new model that inherits from an existing model by specifying the base model class as a parameter to the new class.
  • init method
    • the init method is a special method in Python classes that gets called when an instance of the class is created. It is also known as the constructor method.

The purpose of the init method is to initialize the attributes of the object to their initial state. This is where you can set default values for attributes, and accept arguments that can be used to initialize the object's attributes.

Here is an example of a simple class with an __ini

  • @property, @.setter</li>
  • additional methods
  • </ul> </div> </div> </div>
    """ database dependencies to support sqlite examples """
    import datetime
    from datetime import datetime
    import json
    
    from sqlalchemy.exc import IntegrityError
    from werkzeug.security import generate_password_hash, check_password_hash
    
    
    ''' Tutorial: https://www.sqlalchemy.org/library.html#tutorials, try to get into a Python shell and follow along '''
    
    # Define the User class to manage actions in the 'users' table
    # -- Object Relational Mapping (ORM) is the key concept of SQLAlchemy
    # -- a.) db.Model is like an inner layer of the onion in ORM
    # -- b.) User represents data we want to store, something that is built on db.Model
    # -- c.) SQLAlchemy ORM is layer on top of SQLAlchemy Core, then SQLAlchemy engine, SQL
    
    import sqlite3
    
    # Define a Book class
    class Book:
        def __init__(self, title, author, year_published, isbn):
            self.title = title
            self.author = author
            self.year_published = year_published
            self.isbn = isbn
    
    # Create a connection to the database
    conn = sqlite3.connect('books.db')
    
    # Create a cursor object
    c = conn.cursor()
    
    # Create a table to store books
    c.execute('''CREATE TABLE books
                 (title text, author text, year_published int, isbn text)''')
    
    # Define some sample books
    b1 = Book('To Kill a Mockingbird', 'Harper Lee', 1960, '9780061120084')
    b2 = Book('1984', 'George Orwell', 1949, '9780451524935')
    b3 = Book('Pride and Prejudice', 'Jane Austen', 1813, '9780486284736')
    
    # Add the sample books to the database
    books = [b1, b2, b3]
    for book in books:
        c.execute("INSERT INTO books VALUES (?, ?, ?, ?)",
                  (book.title, book.author, book.year_published, book.isbn))
    
    # Save changes to the database
    conn.commit()
    
    # Close the connection to the database
    conn.close()
    
    ---------------------------------------------------------------------------
    OperationalError                          Traceback (most recent call last)
    /home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a copy.ipynb Cell 6 in <cell line: 35>()
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=31'>32</a> c = conn.cursor()
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=33'>34</a> # Create a table to store books
    ---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=34'>35</a> c.execute('''CREATE TABLE books
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=35'>36</a>              (title text, author text, year_published int, isbn text)''')
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=37'>38</a> # Define some sample books
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=38'>39</a> b1 = Book('To Kill a Mockingbird', 'Harper Lee', 1960, '9780061120084')
    
    OperationalError: table books already exists

    Initial Data

    Uses SQLALchemy db.create_all() to initialize rows into sqlite.db

    • Comment on how these work?
    1. Create All Tables from db Object All items are created using the DB object
    2. User Object Constructors Object constructors create the specific columns and such
    3. Try / Except Try this... except if something happens, do this...

    Check for given Credentials in users table in sqlite.db

    Use of ORM Query object and custom methods to identify user to credentials uid and password

    • Comment on purpose of following
    1. User.query.filter_by
      • This function will essentially filter out all of the user ids and will be a check for any specific calls made in the db
    2. user.password
      • Creates password?? (Unknown, wasn't here)
    def find_by_book_name(title):
        with app.app_context():
            book = Book.query.filter_by(_title=title).first()
        return book # returns user object
    
    # Check credentials by finding user and verify password
    def check_credentials(uid, password):
        # query email and return user record
        user = find_by_uid(uid)
        if user == None:
            return False
        if (user.is_password(password)):
            return True
        return False
            
    #check_credentials("indi", "123qwerty")
    
    import sqlite3
    db = "/Users/ederickwong/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/instance/books.db"
    
    # Define the Book class
    class Book:
        def __init__(self, title, author, year_published, isbn):
            self.title = title
            self.author = author
            self.year_published = year_published
            self.isbn = isbn
    
    # Define a function to display all the books in the database
    def display_books():
        with sqlite3.connect(db) as conn:
            c = conn.cursor()
            c.execute("SELECT * FROM books")
            books = c.fetchall()
            if books:
                for book in books:
                    print(f"{book[0]} - {book[1]} by {book[2]}, published in {book[3]}. ISBN: {book[4]}")
            else:
                print("There are no books in the database.")
    
    # Define a function to add a new book to the database
    def add_book():
        title = input("Enter the title of the book: ")
        author = input("Enter the author of the book: ")
        year_published = input("Enter the year the book was published: ")
        isbn = input("Enter the ISBN of the book: ")
        new_book = Book(title, author, year_published, isbn)
        with sqlite3.connect(db) as conn:
            c = conn.cursor()
            c.execute("INSERT INTO books VALUES (?, ?, ?, ?)", (new_book.title, new_book.author, new_book.year_published, new_book.isbn))
            print(f"{new_book.title} by {new_book.author} has been added to the database.")
    
    # Define a function to update an existing book in the database
    def update_book():
        isbn = input("Enter the ISBN of the book you want to update: ")
        with sqlite3.connect(db) as conn:
            c = conn.cursor()
            c.execute("SELECT * FROM books WHERE isbn=?", (isbn,))
            book = c.fetchone() # fetchone() Method. This method retrieves the next row of a query result set and returns a single sequence, or None if no more rows are available.
            if book:
                print(f"You are updating {book[0]} by {book[1]}")
                title = input("Enter the new title of the book (leave blank to keep the same): ")
                author = input("Enter the new author of the book (leave blank to keep the same): ")
                year_published = input("Enter the new year the book was published (leave blank to keep the same): ")
                if title:
                    c.execute("UPDATE books SET title=? WHERE isbn=?", (title, isbn))
                if author:
                    c.execute("UPDATE books SET author=? WHERE isbn=?", (author, isbn))
                if year_published:
                    c.execute("UPDATE books SET year_published=? WHERE isbn=?", (year_published, isbn))
                print(f"{book[0]} by {book[1]} has been updated in the database.")
            else:
                print("No book with that ISBN was found in the database.")
    
    # Define a function to delete an existing book from the database
    def delete_book():
        isbn = input("Enter the ISBN of the book you want to delete: ")
        with sqlite3.connect(db) as conn:
            c = conn.cursor()
            c.execute("SELECT * FROM books WHERE isbn=?", (isbn,))
            book = c.fetchone()
            if book:
                confirm = input(f"Are you sure you want to delete {book[0]} by {book[1]}? (y/n)")
                if confirm.lower() == "y":
                    c.execute("DELETE FROM books WHERE isbn=?", (isbn,))
                    print(f"{book[0]} by {book[1]} has been deleted from the database.")
            else:
                print("No")
    update_book()
    
    ---------------------------------------------------------------------------
    OperationalError                          Traceback (most recent call last)
    /home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a copy.ipynb Cell 10 in <cell line: 72>()
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=69'>70</a>         else:
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=70'>71</a>             print("No")
    ---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=71'>72</a> update_book()
    
    /home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a copy.ipynb Cell 10 in update_book()
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=36'>37</a> def update_book():
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=37'>38</a>     isbn = input("Enter the ISBN of the book you want to update: ")
    ---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=38'>39</a>     with sqlite3.connect(db) as conn:
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=39'>40</a>         c = conn.cursor()
         <a href='vscode-notebook-cell://wsl%2Bubuntu/home/b3nto4lunch/vscode/Ederick-s-2022-2023-APCSP-Blog/_notebooks/2023-03-13-AP-unit2-4a%20copy.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=40'>41</a>         c.execute("SELECT * FROM books WHERE isbn=?", (isbn,))
    
    OperationalError: unable to open database file

    In this code, we can see that the program is organized into four functions: display_books(), add_book(), update_book(), and delete_book(). Each function performs a specific task related to managing a book library database. For example, display_books() retrieves all the books from the database and displays them, while add_book() allows the user to input information for a new book and adds it to the database.

    Each function has a specific purpose and can be called or executed from other parts of the program. This helps to keep the code organized and easier to understand and maintain. Additionally, by using functions, the code can be reused in other programs, making it more modular and scalable.

    Create a new User in table in Sqlite.db

    Uses SQLALchemy and custom user.create() method to add row.

    • Comment on purpose of following
    1. user.find_by_uid() and try/except
      • this command tries to find user based on the ID. This should theoretically increase the ease of access when accesses large database files and trying to locate specifc information.
    2. user = User(...)
      • This command should defind the user as whatever the input is.
    3. user.dob and try/except
    4. user.create() and try/except
      • Creates a specific user if all traits are qualified.

    Reading users table in sqlite.db

    Uses SQLALchemy query.all method to read data

    • Comment on purpose of following
    1. User.query.all
    2. json_ready assignment

    User.query.all() is a SQLAlchemy query that retrieves all instances of the User model from the database. In this code, it retrieves all users from the database.

    json_ready is a list comprehension that creates a new list by iterating through each user in the table (which contains all users from the database) and calling the read() method on each user. The read() method of each User object would be responsible for creating a JSON representation of that object. This list of JSON objects is assigned to the json_ready variable, which is then returned by the read() function.

    # SQLAlchemy extracts all users from database, turns each user into JSON
    def read():
        with app.app_context():
            table = User.query.all()
        json_ready = [user.read() for user in table] # each user adds user.read() to list
        return json_ready
    
    read()
    
    [{'id': 1,
      'name': 'Thomas Edison',
      'uid': 'toby',
      'dob': '02-11-1847',
      'age': 176},
     {'id': 2,
      'name': 'Nikola Tesla',
      'uid': 'niko',
      'dob': '03-15-2023',
      'age': 0},
     {'id': 3,
      'name': 'Alexander Graham Bell',
      'uid': 'lex',
      'dob': '03-15-2023',
      'age': 0},
     {'id': 4,
      'name': 'Eli Whitney',
      'uid': 'whit',
      'dob': '03-15-2023',
      'age': 0},
     {'id': 5,
      'name': 'Indiana Jones',
      'uid': 'indi',
      'dob': '10-21-1920',
      'age': 102},
     {'id': 6,
      'name': 'Marion Ravenwood',
      'uid': 'raven',
      'dob': '10-21-1921',
      'age': 101}]

    Hacks

    • Add this Blog to you own Blogging site. In the Blog add notes and observations on each code cell.
    • Add Update functionality to this blog.
    • Add Delete functionality to this blog.
    import sqlite3
    database = 'instance/sqlite.db' 
    
    def delete():
        # Get the id of the row to delete
        row_id = input("Enter the id of the row to delete: ")
    
        # Connect to the database file
        conn = sqlite3.connect(database)
    
        # Create a cursor object to execute SQL commands
        cursor = conn.cursor()
    
        try:
            # Execute the DELETE command using the id column
            cursor.execute("DELETE FROM users WHERE id = ?", (row_id,))
    
            if cursor.rowcount == 0:
                # The row with the given id was not found in the table
                print(f"No row with id {row_id} was found in the table")
            else:
                # The row was successfully deleted
                print(f"The row with id {row_id} was successfully deleted from the table")
        except sqlite3.Error as error:
            print(f"Error deleting row: {error}")
        finally:
            # Commit the changes and close the database connection
            conn.commit()
            conn.close()
            
            
    delete()
    
    The row with id 1 was successfully deleted from the table
    

    Does this show procedural abstraction? if so, explain:

    Yes, this code shows procedural abstraction. Procedural abstraction is a programming concept that involves breaking down a program into smaller, more manageable tasks, each of which can be executed as a procedure or function. In this code, each function (display_books, add_book, update_book, and delete_book) represents a specific task or operation that can be performed on the database, and each function encapsulates the details of that operation. This allows the main program or other parts of the program to interact with the database using a simple function call, without needing to know the details of how the operation is performed. Procedural abstraction helps to simplify complex programs and makes them easier to maintain and modify.

    </div>