Hopefully, you figured out how to solve the challenge from the last lesson, as a review, here's a summary of some of the things we did:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///<name of database>.db"
db = SQLAlchemy(app)
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(250), unique=True, nullable=False)
author = db.Column(db.String(250), nullable=False)
rating = db.Column(db.Float, nullable=False
db.create_all()
In addition to these things, the most crucial thing to figure out when working with any new database technology is how to CRUD data records.
Create
Read
Update
Delete
So, let's go through each of these using SQLite and SQLAlchemy:
new_book = Book(id=1, title="Harry Potter", author="J. K. Rowling", rating=9.3)
db.session.add(new_book)
db.session.commit()
NOTE: When creating new records, the primary key fields is optional. you can also write:
new_book = Book(title="Harry Potter", author="J. K. Rowling", rating=9.3)
the id field will be auto-generated.
all_books = session.query(Book).all()
book = Book.query.filter_by(title="Harry Potter").first()
book_to_update = Book.query.filter_by(title="Harry Potter").first()
book_to_update.title = "Harry Potter and the Chamber of Secrets"
db.session.commit()
book_id = 1
book_to_update = Book.query.get(book_id)
book_to_update.title = "Harry Potter and the Goblet of Fire"
db.session.commit()
book_id = 1
book_to_delete = Book.query.get(book_id)
db.session.delete(book_to_delete)
db.session.commit()
You can also delete by querying for a particular value e.g. by title or one of the other properties.