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. Use Debugging through these examples to examine Objects created in Code.

  • 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? Provide a defintion of purpose.
    1. Flask app object
      • we've used this in our CPT flask, for example we have used the init_app() method and can use the request and response objects.
    2. SQLAlchemy db object
      • these are the objects from flask import Flaskfrom flask_sqlalchemy import SQLAlchemy

Notes input

  • OOP programming: User class with objects and functions built into it (with attributes)
  • Imperative: basic level, functions by function (today)
  • schema (in a database): columns of a database, how you describe the database
    • purpose: differentiate between the columns, easily extract data (organized)
  • primary key: should be unique, userid is best (people can share names)
  • datatypes in SQL: string, boolean, integer, images, list, dictionary, class
"""
These imports define the key objects
"""
import datetime
from datetime import datetime
import json
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from sqlalchemy.exc import IntegrityError
from werkzeug.security import generate_password_hash, check_password_hash

"""
These object and definitions are used throughout the Jupyter Notebook.
"""

# 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, purpose and definition.

    • class User
    • db.Model inheritance
    • init method
    • @property, @<column>.setter
    • create, read, update, delete methods
  • We need a constructor, init, method to create a User class

  • attributes:name, uid, password, dob- db.Model is an attribute of User class, inheriting data/functions in db.Model to build the User class
  • @property, getter, Create or Update (CRUD) --> add a user in db
  • @"name".setter, setter, (CRUD)
import datetime
from datetime import datetime
import json

from sqlalchemy.exc import IntegrityError
from werkzeug.security import generate_password_hash, check_password_hash

class Trip(db.Model):
    __tablename__ = 'Trips'  # table name is plural, class name is singular
    __table_args__ = {'extend_existing': True}   # __abstract__ = True

    # Define the User schema with "vars" from object
    id = db.Column(db.Integer, primary_key=True)
    _name = db.Column(db.String(255), unique=False, nullable=False)
    _uid = db.Column(db.String(255), unique=True, nullable=False)
    _password = db.Column(db.String(255), unique=True, nullable=False)
    _destination = db.Column(db.String(255), unique=True, nullable=False)
    _budget = db.Column(db.String(255), unique=False, nullable=False)
    _dob= db.Column(db.Date, unique=True, nullable=False)
    ## add duration of trip

    # constructor of a User object, initializes the instance variables within object (self)
    def __init__(self, name, uid, password="123elly", destination="Dubai", budget="$500", dob=datetime.today()):
        self._name = name    # variables with self prefix become part of the object, 
        self._uid = uid
        self._password = password
        self._destination = destination
        self._budget = budget
        if isinstance(dob, str):  # not a date type     
            dob = date=datetime.today()
        self._dob = dob

    # a name getter method, extracts name from object
    @property
    def name(self):
        return self._name
    
    # a setter function, allows name to be updated after initial object creation
    @name.setter
    def name(self, name):
        self._name = name
    
    @property
    def uid(self):
        return self._uid
    
    # a setter function, allows uid to be updated after initial object creation
    @uid.setter
    def uid(self, uid):
        self._uid = uid
        
    # check if uid parameter matches user id in object, return boolean
    def is_uid(self, uid):
        return self._uid == uid

    # a getter method, extracts uid from object
    @property
    def destination(self):
        return self._destination
    
    # a setter function, allows uid to be updated after initial object creation
    @destination.setter
    def destination(self, destination):
        self._destination = destination

    @property
    def budget(self):
        return self._budget 
    
    # a setter function, allows name to be updated after initial object creation
    @budget.setter
    def budget(self, budget):
        self._budget = budget
    
    @property
    def password(self):
        return self._password[0:10] + "..." # because of security only show 1st characters

    # update password, this is conventional method used for setter
    def set_password(self, password):
        """Create a hashed password."""
        self._password = generate_password_hash(password, method='sha256')

    # check password parameter against stored/encrypted password
    def is_password(self, password):
      #  """Check against hashed password."""
        result = check_password_hash(self._password, password)
        return result
    
    # dob property is returned as string, a string represents date outside object
    @property
    def dob(self):
        dob_string = self._dob.strftime('%m-%d-%Y')
        return dob_string
    
    # dob setter, verifies date type before it is set or default to today
    @dob.setter
    def dob(self, dob):
        if isinstance(dob, str):  # not a date type     
            dob = date=datetime.today()
        self._dob = dob
    # age is calculated field, age is returned according to date of birth
    # @property
    #def age(self):
      #  today = datetime.today()
       # return today.year - self._dob.year - ((today.month, today.day) < (self._dob.month, self._dob.day))
    
    # output content using str(object) is in human readable form
    # output content using json dumps, this is ready for API response
    def __str__(self):
        return json.dumps(self.read())

    # CRUD create/add a new record to the table
    # returns self or None on error
    def create(self):
        try:
            # creates a person object from User(db.Model) class, passes initializers
            db.session.add(self)  # add prepares to persist person object to Users table
            db.session.commit()  # SqlAlchemy "unit of work pattern" requires a manual commit
            return self
        except IntegrityError:
            db.session.remove()
            return None

    # CRUD read converts self to dictionary
    # returns dictionary
    def read(self):
        return {
            "id": self.id,
            "name": self.name,
            "uid": self.uid,
            "password": self.password,
            "destination": self.destination,
            "budget": self.budget,
            "date of birth": self.dob
        }

    # CRUD update: updates user name, password, phone
    # returns self
    def update(self, name="", uid="", password="", destination="", budget="", dob=""):
        """only updates values with length"""
        if len(name) > 0:
            self.name = name
        if len(uid) > 0:
            self.uid = uid
        if len(password) > 0:
            self.set_password(password)
        if len(destination) > 0:
            self.destination = destination
        if len(budget) > 0:
            self.budget = budget
        if len(dob) > 0:
            self.dob = dob
        db.session.commit()
        return self

    # CRUD delete: remove self
    # None
    def delete(self):
        db.session.delete(self)
        db.session.commit()
        return None
        

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
      • generates database tables based on a defined schema within a database object. Automates the creation of database tables base.
    2. User Object Constructors
      • converts the inputs into an object
    3. Try / Except
      • allows your program to take alternative actions in case an error occurs. Python will first attempt to execute the code in the try statement (code block 1). If no exception occurs, the except statement is skipped and the execution of the try statement is finished.

Notes

  • inside initUsers, you add attributes in when creating the user (name, uid...)
  • users are stored in a list
  • object = user.create() goes through each user and adds to SQLite Table
def initTrips():
    with app.app_context():
        """Create database and tables"""
        db.create_all()
        """Tester data for table"""
        u1 = Trip(name='Joselyn Anda', uid='jesa06', password='123Oakllyna', destination='La Jolla Cove', budget='$150000', dob=datetime(2006, 2, 11))
        u2 = Trip(name='Naja Fonseca', uid='najapotato', password='purplepotato', destination='Santee Lakes', budget='$1200', dob=datetime(2008, 7, 18))
        u3 = Trip(name='Lina Awad ', uid='linaawad7', password='thomasT3', destination='Bahia Resort', budget='$30750', dob=datetime(2005, 6, 23))
        u4 = Trip(name='Amitha Sanka', uid='aamithass', password='adventurous11', destination='San Diego Zoo', budget='none',dob=datetime(2004, 10, 21))


        users = [u1, u2, u3, u4]

        """Builds sample user/note(s) data"""
        for theTrip in users:
            try:
                '''add user to table'''
                object = Trip.create()
                print(f"Created new uid {object.uid}")
            except:  # error raised if object nit created
                '''fails with bad or duplicate data'''
                print(f"Records exist uid {Trip.uid}, or error.")
                
initTrips()
Records exist uid <property object at 0x7fa2a0be7630>, or error.
Records exist uid <property object at 0x7fa2a0be7630>, or error.
Records exist uid <property object at 0x7fa2a0be7630>, or error.
Records exist uid <property object at 0x7fa2a0be7630>, or error.

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
      • query:a search to find something 2. user.password
        • checks the password of the user in the database
    2. check_credentials(uid, password), use one object to find another object (check if userid is same --> checks if password is same, if userid is not matching, the function does not happen) 2 layers of checking, each parameter
def find_by_uid(uid):
    with app.app_context():
        trip = Trip.query.filter_by(_uid=uid).first()
    return trip # returns user object
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("jesa06", "123Oakllyna")
False

Create a new User in Trips 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
      • check if input uid matches another uid in the database
      • allows program to continue to work through errors and lets the user know if data is found in the database
    2. user = User(...)
      • initializes certain attributes and data that is given from the user input
    3. user.dob and try/except
      • sets the dob to the new created user, in the date form
      • if the date is not given or typed in incorrectly the dob is set to today
    4. user.create() and try/except
      • the object, the function takes all of this input data to create another user and add it to the database
      • except happens if an errored occurred and the program could not create another user
def create():
    # optimize user time to see if uid exists
    uid = input("Enter your user id:")
    user = find_by_uid(uid)
    try:
        print("Found\n", user.read())
        return
    except:
        pass # keep going
    
    # request value that ensure creating valid object
    name = input("Enter your name:")
    password = input("Enter your password:")
    destination = input("Enter your desired trip destination:")
    budget = input("Enter your budget:")
    
    # Initialize User object before date
    user = Trip(name=name, 
                uid=uid, 
                password=password,
                destination=destination,
                budget=budget
                )
    
    # create user.dob, fail with today as dob
    dob = input("Enter your date of birth 'YYYY-MM-DD'")
    try:
       user.dob = datetime.strptime(dob, '%Y-%m-%d').date()
    except ValueError:
       user.dob = datetime.today()
       print(f"Invalid date {dob} require YYYY-mm-dd, date defaulted to {user.dob}")
           
    # write object to database
    with app.app_context():
        try:
            object = user.create()
            print("Created\n", object.read())
        except:  # error raised if object not created
            print("Unknown error uid {uid}")
        
create()
Created
 {'id': 9, 'name': 'Athyna', 'uid': 'athyna7', 'password': 'ilovetopla...', 'destination': 'Dog Beach', 'budget': 'none', 'date of birth': '04-15-2013'}

Reading Trips table in sqlite.db

Uses SQLALchemy query.all method to read data

  • Comment on purpose of following
    1. User.query.all
      • searches for all of the attributes and data within the Trip table
    2. json_ready assignment, google List Comprehension
      • we use json because you can convert into each language, it is a universal translator
def read():
    with app.app_context():
        table = Trip.query.all()
    json_ready = [trip.read() for trip in table] # "List Comprehensions", for each user add user.read() to list
    return json_ready

read()
[{'id': 2,
  'name': 'Nikola Tesla',
  'uid': 'ntesla2',
  'password': 'modelx...',
  'destination': 'La Jolla Cove',
  'budget': '$50000',
  'date of birth': '03-14-2023'},
 {'id': 4,
  'name': 'Marion Ravenwood',
  'uid': 'ravenrion',
  'password': 'blackmagic...',
  'destination': 'Air and Space Museum',
  'budget': '$725000',
  'date of birth': '10-21-1921'},
 {'id': 5,
  'name': 'Joselyn Anda',
  'uid': 'jesa06',
  'password': '123EllynaO...',
  'destination': 'Hooters',
  'budget': '$50',
  'date of birth': '12-21-2006'},
 {'id': 7,
  'name': 'Eli Whitney',
  'uid': 'eliwhi',
  'password': 'Greenbay4...',
  'destination': 'Bahia Resort',
  'budget': '$30750',
  'date of birth': '06-23-2002'},
 {'id': 8,
  'name': 'Elly Anda',
  'uid': 'ellyaa',
  'password': 'ilovefood...',
  'destination': 'Home',
  'budget': 'none',
  'date of birth': '01-19-2012'},
 {'id': 9,
  'name': 'Athyna',
  'uid': 'athyna7',
  'password': 'ilovetopla...',
  'destination': 'Coronado Dog Beach',
  'budget': 'none',
  'date of birth': '04-15-2013'},
 {'id': 10,
  'name': 'Derryck Anda',
  'uid': 'derrycka',
  'password': 'f@mily1...',
  'destination': 'Philippines',
  'budget': '$25',
  'date of birth': '11-11-1998'}]

Updating "destination" in Trips table in sqlite.db

Updating using object functions

def update():
    # optimize user time to see if uid exists
    uid = input("Enter your user id:")
    user = find_by_uid(uid)
    try:
        print("Found\n", user.read())
    except:
        pass # keep going
    
    destination = input("Enter your new destination:")

    user.destination=destination
    
    # write object to database
    with app.app_context():
        try:
            object = user.update()
            print("Updated\n", object.read())
        except:  # error raised if object not created
            print("Unknown error uid {uid}")
        
update()
Found
 {'id': 9, 'name': 'Athyna', 'uid': 'athyna7', 'password': 'ilovetopla...', 'destination': 'Coronado Dog Beach', 'budget': 'none', 'date of birth': '04-15-2013'}
Updated
 {'id': 9, 'name': 'Athyna', 'uid': 'athyna7', 'password': 'ilovetopla...', 'destination': 'Home', 'budget': 'none', 'date of birth': '04-15-2013'}

Deleting users table in sqlite.db

Uses SQLALchemy

def delete():
    # optimize user time to see if uid exists
    uid = input("Enter the user id you want to delete:")
    user = find_by_uid(uid)
    try:
        print("Found\n", user.read())
    except:
        pass # keep going
       
    
    # write object to database
    with app.app_context():
        try:
            user.delete()
            print("Deleted\n", uid)
        except:  # error raised if object not created
            print("Unknown error uid {uid}")
        
delete()
Found
 {'id': 1, 'name': 'Thomas Edison', 'uid': 'tommye', 'password': 'lightbulb...', 'destination': 'SeaWorld', 'budget': '$150000', 'date of birth': '02-11-1847'}
Deleted
 tommye

Debugging

Within initUsers() function there are different types of variables. We have class variables which are shared by all instances of this User class.

from IPython.display import Image, display
from pathlib import Path  # https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f

# prepares a series of images
def image_data(path=Path("images/"), images=None):  # path of static images is defaulted
    if images is None:  # default image
        images = [
            {'source': "My laptop", 'label': "Debugging Read Function", 'file': "debug2.4a.png"}
            ]
    for image in images:
        # File to open
        image['filename'] = path / image['file']  # file with path
    return images

def image_display(images):
    for image in images:  
        display(Image(filename=image['filename']))


# Run this as standalone tester to see sample data printed in Jupyter terminal
if __name__ == "__main__":

    
    # display default images from image_data()
    default_images = image_data()
    image_display(default_images)