Professional Documents
Culture Documents
CRUD Sqlite Alchemy
CRUD Sqlite Alchemy
1. class Book(db.Model):
2. id = db.Column(db.Integer, primary_key=True)
3. title = db.Column(db.String(250), unique=True, nullable=False)
4. author = db.Column(db.String(250), nullable=False)
5. rating = db.Column(db.Float, nullable=False
6.
7. 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 data records.
Create
Read
Update
Delete
NOTE: When creating new records, the primary key fields is optional. you can also write:
1. all_books = session.query(Book).all()
1. book_id = 1
2. book_to_update = Book.query.get(book_id)
3. book_to_update.title = "Harry Potter and the Goblet of Fire"
4. db.session.commit()
1. book_id = 1
2. book_to_delete = Book.query.get(book_id)
3. db.session.delete(book_to_delete)
4. db.session.commit()
You can also delete by querying for a particular value e.g. by title or one of the other
properties.