Python SQLAlchemy Basics Introduction to SQLAlchemy Querying the Movie Database

Anthony Piscocama
Anthony Piscocama
2,344 Points

The movie theater has added all of their current offerings to the database you created, but they need some help finding

i am not sure where i am going wrong.
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine(sqlite:///movies.db, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()

class Movie(Base):
    __tablename__ = movies

    id = Column(Integer, primary_key=True)
    movie_title = Column(String)
    genre = Column(String)

# Write your code below
for movie in session.query(Movie).filter_by(genre=='Romance'):
    romance_movies = movie

1 Answer

Megan Amendola
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

Hi! The query you have already holds all of the romance movies, so you just need to set it equal to the variable name.

romance_movies = session.query(Movie).filter_by(genre=='Romance')

Right now your for loop is overriding the variable with a new movie each time. For example, if the movies it returned were 'The Notebook', 'Romancing the Stone', and 'Titanic'. The loop will grab the first movie, 'The Notebook' and set it equal to the variable, then it will grab the next one, 'Romancing the Stone', and set the variable to now equal this movie, and finally, it will grab the last movie, 'Titanic', and set the variable equal to this last movie. So at the end of the loop, the variable equals the final movie, 'Titanic', instead of the whole list of movies.