unexpected results from query

17 views
Skip to first unread message

Cravan

unread,
Jun 24, 2019, 11:34:52 PM6/24/19
to sqlalchemy

Hi all,    

                I’m working on the same movie review project as before, but I recently tried to create a new search module for my search function, which is to search for the movie’s imdbid. Each movie has its own unique imdbid, but when I search for a single unique imdbid, somehow all the movies pop out. Can someone point out the source of the problem? On a side note, I tried to include a print statement in each condition statement, but nothing is printed, and I tried fetchone instead of fetchall, but it didn’t work either. Thanks in advance!

Here is my results function:

````

@app.route("/movie_results")

def movie_results():

    name = request.args.get("movie.title")

    year = request.args.get("movie.year")

    imdbid = request.args.get("movie.imdbid")

    name_pattern = "%" + name + "%"

    if year == '' or None and imdbid == '' or None:

        search_movie_statement = sqlalchemy.text('SELECT * FROM movies WHERE title ILIKE :movie_title')

        movie_specific = engine.execute(search_movie_statement, movie_title=name_pattern).fetchall()

        if len(movie_specific) != 0 and movie_specific is not None:

            return render_template("movie_specific.html", movie_specific=movie_specific)

        if len(movie_specific) == 0:

            return render_template("error2.html", message="No such movie.")

    elif name == '' or None and imdbid == '' or None:

        search_movie_statement = sqlalchemy.text('SELECT * FROM movies WHERE year = :year')

        movie_specific = engine.execute(search_movie_statement, year=year).fetchall()

        if len(movie_specific) != 0 and movie_specific is not None:

            return render_template("movie_specific.html", movie_specific=movie_specific)

        if len(movie_specific) == 0:

            return render_template("error2.html", message="No such movie.")

 

    elif name == '' or None and year == '' or None:

        search_movie_statement = sqlalchemy.text('SELECT * FROM movies WHERE "imdbID" = :imdbId')

        movie_specific = engine.execute(search_movie_statement, imdbId=imdbid).fetchall()

        if len(movie_specific) != 0 and movie_specific is not None:

            return render_template("movie_specific.html", movie_specific=movie_specific)

        if len(movie_specific) == 0:

            return render_template("error2.html", message="No such movie.")

 

    elif name == '' or None:

        print(name)

        search_movie_statement = sqlalchemy.text('SELECT * FROM movies WHERE "imdbID" = :imdbid AND year = :year')

        movie_specific = engine.execute(search_movie_statement, imdbid=imdbid, year=year).fetchall()

        if len(movie_specific) != 0 and movie_specific is not None:

            return render_template("movie_specific.html", movie_specific=movie_specific)

        if len(movie_specific) == 0:

            return render_template("error2.html", message="No such movie.")

 

    elif year == '' or None:

        search_movie_statement = sqlalchemy.text('SELECT * FROM movies WHERE "imdbID" = :imdbid and title ILIKE :movie_title')

        movie_specific = engine.execute(search_movie_statement, imdbid=imdbid, movie_title=name_pattern).fetchall()

        if len(movie_specific) != 0 and movie_specific is not None:

            return render_template("movie_specific.html", movie_specific=movie_specific)

        if len(movie_specific) == 0:

            return render_template("error2.html", message="No such movie.")

 

    elif imdbid == '' or None:

        search_movie_statement = sqlalchemy.text('SELECT * FROM movies WHERE year = :year and title ILIKE :movie_title')

        movie_specific = engine.execute(search_movie_statement, year=year, movie_title=name_pattern).fetchall()

        if len(movie_specific) != 0 and movie_specific is not None:

            return render_template("movie_specific.html", movie_specific=movie_specific)

        if len(movie_specific) == 0:

            return render_template("error2.html", message="No such movie.")

 

    elif name != None and name != '' and year != '' and year != None and imdbid != '' and imdbid != None:

        search_movie_statement = sqlalchemy.text('SELECT * FROM movies WHERE year = :year and title ILIKE :movie_title and imdbID = :imdbid')

        movie_specific = engine.execute(search_movie_statement, year=year, movie_title=name_pattern, imdbid=imdbid).fetchall()

        if len(movie_specific) != 0 and movie_specific is not None:

            return render_template("movie_specific.html", movie_specific=movie_specific)

        if len(movie_specific) == 0:

            return render_template("error2.html", message="No such movie.")

````

Here is my movie table:

````

CREATE TABLE movies (

      "title" TEXT UNIQUE NOT NULL,

      "year" INTEGER NOT NULL,

      "runtime" INTEGER NOT NULL,

      "imdbID" VARCHAR NOT NULL,

      "imdbRating" NUMERIC NOT NULL

  );

````

Thanks,

Cravan

Simon King

unread,
Jul 2, 2019, 6:11:18 AM7/2/19
to sqlal...@googlegroups.com
Statements like this don't do what you are expecting:

elif year == '' or None:

"==" has higher operator precedence than "or", so this is interpreted as:

elif (year == '') or None:

...which is the same as:

elif year == '':

This one is more complicated:

if year == '' or None and imdbid == '' or None:

"==" is still the highest precedence, followed by "and", and then
"or", so I think this is evaluated as:

if (year == '') or (None and (imdbid == '')) or None

"None and <anything>" will always be None, so this is actually the same as:

if (year == '') or None or None:

...which is equivalent to:

if (year == ''):

If you want to test if a value is either an empty string or None, you
would use one of these forms:

if (year == '') or (year is None):

Or:

if year in ('', None):

Hope that helps,

Simon
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/A18038FC-74A8-4C04-9147-69CD429A1E75%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages