https://classroom.udacity.com/courses/ud088
Lesson 1: CRUD
Preparation
- Install Git
- Install Virtual Box
- Install Vagrant
- Fork the starter repo: https://github.com/udacity/fullstack-nanodegree-vm
cd fullstack/vagrant
vagrant up
vagrant ssh
CRUD
- Create (SQL Keyword:
INSERT
) - Read (SQL Keyword:
SELECT
) - Update (SQL Keyword:
UPDATE
) - Delete (SQL Keyword:
DELETE
)
Creating a Database
Restaurant menu
Using Python means to send SQL statements as strings
A better way is to use Object-Relational Mapping (ORM)
SQLAlchemy
SQLAlchemy is a Python SQL toolkit and Object Relational Mapper.
Configuration
- Generally shouldn’t change from project to project
- At beginning of file:
- imports all modules needed
- creates instance of declarative base
- At end of file
- creates (or connects) the database and adds tables and columns
database_setup.py
import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative imort declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
Base = declarative_base()
###### insert at end of file ######
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.create_all(engine)
Class
- Representation of table as a python class
- extends the Base class
- Nested inside will be table and mapper code
class Restaurant(Base):
class MenuItem(Base):
Table
-
representation of our table inside the database
-
syntax:
__tablename__ = 'some_table'
# inside restaurant class
__tablename__ = 'restaurant'
# inside MenuItem class
__tablename__ = 'menu_item'
Mapper
-
Maps python objects to columns in our database
-
syntax
columnName = Column(attributes, ...)
example attributes:
String(250)
Integer
relationship(Class)
nullable = False
primary_key = True
ForeignKey('some_table.id')
# inside Restaurant Class
name = Column(String(80), nullable = False)
id = Column(Integer, primary_key = True)
# inside MenuItem Class
name = Column(String(80), nullable = False)
id = Column(Integer, primary_key = True)
course = Column(String(250))
description = Column(String(250))
price = Column(String(8))
restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
restaurant = Relationship(Restaurant)
Put it All Together
https://github.com/udacity/Full-Stack-Foundations/blob/master/Lesson_1/database_setup.py
CRUD Create
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, Menu Item
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
A session is…
>>> session = DBSession()
>>> myFirstRestaurant = Restaurant(name = "Pizza Palace")
>>> session.add(myFirstRestaurant)
>>> session.commit()
>>> session.query(Restaurant).all*()
CRUD Read
https://github.com/udacity/Full-Stack-Foundations/blob/master/Lesson_1/lotsofmenus.py
Check out the query documentation for SQLAlchemy
here.
session.query(MenuItem).all()
session.query(Restaurant).all()
CRUD Update
- Find Entry
- Reset value(s)
- Add to session
- Execute
session.commit()
>>> UrbanVeggieBurger = session.query(MenuItem).filter_by(id = 8).one()
>>> print UrbanVeggieBurger.price
$5.99
>>> UrbanVeggieBurger.price = "$2.99"
>>> session.add(UrbanVeggieBruger)
>>> session.commit()
>>> veggieBurgers = session.query(MenuItem).filter_by(name = 'Veggie Burger')
CRUD Delete
- Find entry
session.delete(entry)
session.commit()