GithubHelp home page GithubHelp logo

y2l-databases-sql's Introduction

The Online Shop - Lab (Part 2); Databases


Today, we are continuing creating our awesome online shop! I don't want to see the creativity stop here. Keep it going!

We will be working in the Y2L-Flask-Routing folder from last lab. If you didn't clone the last lab, do so now.

You can also use Today's lecture as reference if you forgot certain lines - https://tinyurl.com/y2-databases

Part 1: Setting Up

  1. Create 2 new empty files and save them as "model.py" and "databases.py"

Part 2: Creating Our Table

  1. Edit "model.py" and add these lines in the beginning:
from sqlalchemy import Column, Integer, String, Date, ForeignKey, Float, Boolean, DateTime
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()
  1. Create a table called "Product" with the following variables:
  • Name (String)
  • Price (Float)
  • Picture Link (String)
  • Description (String)

Part 3: Creating Our Database Functions

  1. Edit "databases.py" and add these lines in the beginning:
from model import Base, Product


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


engine = create_engine('sqlite:///database.db')
Base.metadata.create_all(engine)
DBSession = sessionmaker(bind=engine)
session = DBSession()
  1. Write functions to control the database in "databases.py", you have to have at least 5 functions:
  • a function that adds a product
  • a function that edits a specific product by id
  • a function that deletes a specific product by id
  • a function that returns all products in the table
  • a function that returns a specific product by id

Part 4: Adding to Cart

  1. Open "model.py" and create a new table called "Cart" that has this one variable:
  • productID

** Don't forget the default "id", it's not the same as the variable we created.
We'll use "productID" later to get it's other values like: name, price, description...etc.

  1. Open "databases.py" and create a new function called "Add_To_Cart" that takes one input which is "productID", and adds this "productID" to the new table you just created.

BONUS:

  • Show products in store.html and link all the functions so it could work properly.
  • Create an extra "admin" page that you can access with a special link that has a dashboard to control the website databse, it could have functions like:
    1. Add Products
    2. Edit Products
    3. Delete Products
    4. Show all products

y2l-databases-sql's People

Contributors

loai17 avatar caboonie avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.