from app import db, Puppy #Create my_puppy = Puppy('Rufus', 5, 'brown', 'male') db.session.add(my_puppy) db.session.commit() #Read all_puppies = Puppy.query.all() print(all_puppies) #select by id puppy_one = Puppy.query.get(1) print(puppy_one.name) #filters - will produce sql code puppy_frankie = Puppy.query.filter_by(name='Frankie') print(puppy_frankie) print(puppy_frankie.all()) #update first_puppy = Puppy.query.get(1) first_puppy.age = 10 db.session.add(first_puppy) db.session.commit() rufus = Puppy.query.filter_by(name='Rufus') rufus.fur = 'orange' db.session.commit() #delete
from app import db, Puppy db.create_all() sam = Puppy('Sammy', 3) frank = Puppy('Frank', 5) print(sam.id) db.session.add_all([sam, frank]) db.session.commit() print(sam.id) print(frank.id)
from app import Puppy, db db.create_all() frank = Puppy('Frankie', 3) sam = Puppy('Sammy', 5) db.session.add_all([frank, sam]) db.session.commit() print(sam) print(frank)
from app import db, Puppy # create rufus = Puppy('rufus', 5) db.session.add(rufus) db.session.commit() # select all all_puppy = Puppy.query.all() print(all_puppy) # select by id sam = Puppy.query.get(1) print(sam) print(sam.name) # select by filter frank = Puppy.query.filter_by(name='frankie') # produce some sql code! print(frank) print(frank.all()) # update frank = Puppy.query.get(2) frank.age = 10 db.session.add(frank) db.session.commit() # delete rufus = Puppy.query.get(3) db.session.delete(rufus) db.session.commit()
from app import db, Puppy db.create_all() #creates all tables, transforms model class to table sam = Puppy('Sammy', 3, 'blue', 'male') frank = Puppy('Frankie', 1, 'red', 'male') cutie = Puppy('Cutie', 1, 'light brown', 'female') print(sam.id) print(frank.id) print(cutie.id) db.session.add(sam) db.session.add(frank) db.session.add(cutie) db.session.commit() print(sam.id) print(frank.id) print(cutie.id)
from app import db, Puppy db.create_all() sam = Puppy('sammy', 3) frank = Puppy('frankie', 4) db.session.add_all([sam, frank]) print(sam.id) print(frank.id) db.session.commit() print(sam.id) print(frank.id)
from app import db, Puppy db.create_all() sam = Puppy('Sammy', 3) frank = Puppy('Frankie', 4) db.session.add_all([sam, frank]) db.session.commit() print(sam.id) print(frank.id)
from app import db, Puppy ### CREATE ### floof = Puppy('Fluffy', 4) db.session.add(floof) db.session.commit() ### READ ### ### GET BY ID ### first = Puppy.query.get(1) print(first) ### GET ALL ### puppies = Puppy.query.all() print(puppies) ### GET USING NAME ### floofy_pup = Puppy.query.filter_by(name="Fluffy").first() print(floofy_pup) ### UPDATE ### f = Puppy.query.get(3) f.age = 7 db.session.add(f) db.session.commit() ### DELETE ### second_pup = Puppy.query.get(2) db.session.delete(second_pup)
from app import db, Puppy # create my_puppy = Puppy('Joe', 52) db.session.add(my_puppy) db.session.commit() # read all_puppies = Puppy.query.all() print(all_puppies) # select by id puppy1 = Puppy.query.get(1) print(puppy1) # filters # produces sqlcode puppy_frankie = Puppy.query.filter_by(name='Frank') print(puppy_frankie.all()) # update print(puppy1) first_puppy = Puppy.query.get(1) first_puppy.age = 10 db.session.add(first_puppy) db.session.commit() print(first_puppy)
from app import db, Puppy db.create_all() brigite = Puppy('Brigite', 3) olie = Puppy('Oliver', 3) akira = Puppy('Akira', 0) print(brigite.id) print(olie.id) print(akira.id) db.session.add_all([brigite, olie, akira]) db.session.commit() print(brigite.id) print(olie.id) print(akira.id)
from app import db, Puppy #### CREATE #### # Create new entry with name Rufus and age 5 my_puppy = Puppy("Rufus", 5) db.session.add(my_puppy) db.session.commit() #### READ #### # Grab all puppies all_puppies = Puppy.query.all() print(all_puppies) print("\n") # Grab by id puppy_one = Puppy.query.get(1) print(puppy_one) print(puppy_one.age) print("\n") # Filters puppy_sam = Puppy.query.filter_by(name="Sammy") # Returns list print(puppy_sam) print("\n") #### UPDATE #### # Change the age of puppuy with id = 1 first_puppy = Puppy.query.get(1)
from app import db, Puppy # create my_puppy = Puppy('Buska', 4) db.session.add(my_puppy) db.session.commit() # read all_puppies = Puppy.query.all() print(all_puppies) # select by id puppy_one = Puppy.query.get(1) print(puppy_one) # fitler puppy_franky = Puppy.query.filter_by(name='Franky') print(puppy_franky.all()) # update first_puppy = Puppy.query.get(1) first_puppy.age = 10 db.session.add(first_puppy) db.session.commit() # delete second_puppy = Puppy.query.get(2) db.session.delete(second_puppy) db.session.commit() all_puppies = Puppy.query.all()
# Import database from app import db, Puppy # Create the tables in the database db.create_all() # Create new entries in the db sam = Puppy("Sammy", 3) frank = Puppy("Frankie", 4) # Check ids (haven't added sam and frank to database, so they should be None) print(sam.id) print(frank.id) # Ids will get created automatically once we add these entries to the DB db.session.add_all([sam, frank]) # Alternative for individual additions: # db.session.add(sam) # db.session.add(frank) # Save changes to the DB db.session.commit() # Check the ids print(sam.id) print(frank.id)