Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python SQLAlchemy Basics Working with SQLAlchemy Analyzing the Movie Database

Challenge Task 1 of 1 The movie theater wants to know how many movies they have in their database

Not sure where to start

models.py
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

Challenge Task 1 of 1 The movie theater wants to know how many movies they have in their database with β€˜The’ in the title. They want to post this fun fact on their social media. Create a new variable called the_movies that holds a query to find all products with β€˜%The%’ in the movie_title and counts the number of returned values.\n*Hint:* Use filter() with .like()

2 Answers

kevin stradtman
kevin stradtman
10,172 Points

I finally figured this out the_movies = session.query(Movie.movie_title).filter(Movie.movie_title.like("%The%")).count()

Steven Parker
Steven Parker
231,236 Points

Start by making the variable and beginning the assignment. You know the value will involve calling the .query method on the session, and using .filter on the movie_title attribute of the Movie, making use of .like as mentioned in the hints.

You might want to review the Analyze Books video, particularly about half way through where a very similar query to find specific book titles is constructed.