Intro to Full-stack Dev Flashcards

1
Q

Lesson 1
- From video 21

Review some of the actions taken in this lesson

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give a run through of CRUD delete

Delete the spinach ice cream from the MenuItem

Video 27 lesson 27:

A

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
  1. # select the spinach icecream> spinach = session.query(MenuItem).filter_by(name = ‘Spinach Ice Cream’).one()
  2. # print out the restaurant it appears in> print spinach.restaurant.name
  3. # now delete it> session.delete(spinach)
    session.commit()
  4. # search for spinach ice cream to see if it was delete> spinach = session.query(MenuItem).filter_by(name = “Spinach Ice Cream”).one()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

1). CRUD Review:

Operations with SQLAlchemy

A

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()
>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

2).CRUD Review:

CREATE:

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

3).CRUD Review:

READ:

A

READ:

3).#CRUD Review:

> firstResult = session.query(Restaurant).first()
firstResult.name

> items = session.query(MenuItem).all()
for item in items:
print item.name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

4).CRUD Review:

UPDATE:

A

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() 
>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

5).CRUD Review:

UPDATE:

A

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()
>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What was Lesson 1 teach you?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly