[Flask] Unexpected result when viewing results from a sql query

Cravan savageapple850 at gmail.com
Tue Jun 25 10:04:49 EDT 2019


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

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20190625/fdf2b438/attachment-0001.html>


More information about the Flask mailing list