Intro to Full-stack Dev Flashcards
Lesson 1
- From video 21
Review some of the actions taken in this lesson
In vagrant, run ‘python’
> from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup_working import Base, Restaurant, MenuItem
engine = create_engine(‘sqlite:///restaurantmenu.db’)
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()
myFirstRestaurant = Restaurant(name = “Pizza Palce”)
session.add(myFirstRestaurant)
session.commit()
session.query(Restaurant).all()
cheesepizza = MenuItem(name = “Cheese Pizza”, description = “Made with all natural ingredients and fresh mozzarella”, course = “Entree”, price = “$8.99”, restaurant = myFirstRestaurant)
session.add(cheesepizza)
session.commit()
session.query(MenuItem).all()
#video 23 (CRUD Read) >firstResult = session.query(Restaurant).first() >firstResult.name >session.query(Restaurant).all() >items = session.query(MenuItems).all() >for item in items: >> print item.name
Give a run through of CRUD delete
Delete the spinach ice cream from the MenuItem
Video 27 lesson 27:
it should say ‘no row was found for one()’
# A run through of CRUD delete Steps - 1 - find (put it in a variable 2- print it out to check to see if you've got it 3- delete and commit 4- search again to see if it's gone
- # select the spinach icecream> spinach = session.query(MenuItem).filter_by(name = ‘Spinach Ice Cream’).one()
- # print out the restaurant it appears in> print spinach.restaurant.name
- # now delete it> session.delete(spinach)
session.commit() - # search for spinach ice cream to see if it was delete> spinach = session.query(MenuItem).filter_by(name = “Spinach Ice Cream”).one()
1). CRUD Review:
Operations with SQLAlchemy
Operations with SQLAlchemy
1). #CRUD Review:
> from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem
engine = create_engine('sqlite:///restaurantMenu.db') Base.metadata.bind=engine DBSession = sessionmaker(bind = engine) session = DBSession() >
2).CRUD Review:
CREATE:
CREATE:
2).#CRUD Review:
> myFirstRestaurant = Restaurant(name = “Pizza Palace”)
session. add(myFirstRestaurant)
sesssion. commit()
We then created a cheese pizza menu item and added it to the Pizza Palace Menu:
> cheesepizza = menuItem(name=”Cheese Pizza”, description = “Made with all natural ingredients and fresh mozzarella”, course=”Entree”, price=”$8.99”, restaurant=myFirstRestaurant)
session. add(cheesepizza)
session. commit()
3).CRUD Review:
READ:
READ:
3).#CRUD Review:
> firstResult = session.query(Restaurant).first()
firstResult.name
> items = session.query(MenuItem).all()
for item in items:
print item.name
4).CRUD Review:
UPDATE:
UPDATE:
4).#CRUD Review:
Find Entry Reset value(s) Add to session Execute session.commit() #
We found the veggie burger that belonged to the Urban Burger restaurant by executing the following query:
> veggieBurgers = session.query(MenuItem).filter_by(name= 'Veggie Burger') for veggieBurger in veggieBurgers: print veggieBurger.id print veggieBurger.price print veggieBurger.restaurant.name print "\n" >
Then we updated the price of the veggie burger to $2.99:
> UrbanVeggieBurger = session.query(MenuItem).filter_by(id=8).one() UrbanVeggieBurger.price = '$2.99' session.add(UrbanVeggieBurger) session.commit() >
5).CRUD Review:
UPDATE:
5). #CRUD Review:
UPDATE:
To delete an item from our database we must follow the following steps:
Find the entry Session.delete(Entry) Session.commit() #
We deleted spinach Ice Cream from our Menu Items database with the following operations:
> spinach = session.query(menuItem).filter_by(name = 'Spinach Ice Cream').one() session.delete(spinach) session.commit() >
What was Lesson 1 teach you?
Lesson 1 taught me how to use a/an Object-relational mapping (ORM) to create a database, and to execute CRUD operations on it.
This sets the foundation for the interactive menu application.
In Lesson 2, I’ll build a web server that will able to perform these CRUD operations based on requests from the user
CRUD = CREATE, READ, UPDATE, DELETE