Block 124: SQLAlchemy ORM Basics
Use SQLAlchemy ORM to interact with databases via Python objects.
Concepts
- from sqlalchemy import create_engine, Column, String, Integer
- Declarative base and model classes
- Session: add, commit, query
- Filtering: session.query(Model).filter()
Code Examples
See exercise below.
Exercise
Define a Student model with SQLAlchemy. Create the table. Insert 3 records via ORM. Query all students with grade > 80 using ORM filter.
Homework
What are the advantages of using an ORM vs raw SQL? What are the disadvantages? Wednesday