Blockchain Nanodegree 预修知识之二:Full Stack Foundations (1)

版权声明:本栏目下的所有文章均为个人学习笔记,部分内容为直接搬运,供学习分享。如有版权问题请联系作者删除。 https://blog.csdn.net/xiaozhenliu/article/details/84192901

https://classroom.udacity.com/courses/ud088

Lesson 1: CRUD

Preparation

  1. Install Git
  2. Install Virtual Box
  3. Install Vagrant
  4. Fork the starter repo: https://github.com/udacity/fullstack-nanodegree-vm
  5. cd fullstack/vagrant
  6. vagrant up
  7. vagrant ssh

CRUD

  • Create (SQL Keyword: INSERT)
  • Read (SQL Keyword: SELECT)
  • Update (SQL Keyword: UPDATE)
  • Delete (SQL Keyword: DELETE)

Creating a Database

Restaurant menu

restaurant-db

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

  1. Find Entry
  2. Reset value(s)
  3. Add to session
  4. 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

  1. Find entry
  2. session.delete(entry)
  3. session.commit()

猜你喜欢

转载自blog.csdn.net/xiaozhenliu/article/details/84192901