How do you suggest I change it then? I import my values are from a csv sheet as per the task requirements. As for the other problem, how should I change it? And may I know why will it not work/break badly? Cravan
On 3/7/19, 10:43 PM, "Simon King" <sqlalchemy@googlegroups.com on behalf of si...@simonking.org.uk> wrote: Look more closely here: 2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie)s 2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine {'movie': "('The Lego Movie'"} 2019-07-03 22:21:47,375 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review') 2019-07-03 22:21:47,376 DEBUG sqlalchemy.engine.base.Engine Row ("('The Lego Movie'", Decimal('1'), 'sms', ' a very cool movie') The value in the "movie" column is literally: ('The Lego Movie' ie. "open parenthesis, single quote, The Lego Movie, single quote" Whatever you're doing to insert data into the database is broken. Things like this also raise red flags: check_for_review = engine.execute(check_for_review_statement, movie=movie_title).fetchall() check_for_review_splitted = str(check_for_review).split(',') You're getting a list of rows, converting them to a string, and then splitting that string on commas. This will break badly in anything but the simplest of cases. Simon On Wed, Jul 3, 2019 at 3:23 PM Cravan <cravan...@gmail.com> wrote: > > Yes, it is. > ```` > 2019-07-03 22:21:43,914 INFO sqlalchemy.engine.base.Engine select version() > 2019-07-03 22:21:43,915 INFO sqlalchemy.engine.base.Engine {} > 2019-07-03 22:21:44,608 DEBUG sqlalchemy.engine.base.Engine Col ('version',) > 2019-07-03 22:21:44,608 DEBUG sqlalchemy.engine.base.Engine Row ('PostgreSQL 11.4 (Ubuntu 11.4-1.pgdg16.04+1 > ) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609, 64-bit',) > 2019-07-03 22:21:44,609 INFO sqlalchemy.engine.base.Engine select current_schema() > 2019-07-03 22:21:44,610 INFO sqlalchemy.engine.base.Engine {} > 2019-07-03 22:21:45,020 DEBUG sqlalchemy.engine.base.Engine Col ('current_schema',) > 2019-07-03 22:21:45,021 DEBUG sqlalchemy.engine.base.Engine Row ('public',) > 2019-07-03 22:21:45,346 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) > AS anon_1 > 2019-07-03 22:21:45,346 INFO sqlalchemy.engine.base.Engine {} > 2019-07-03 22:21:45,670 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60) > ) AS anon_1 > 2019-07-03 22:21:45,671 INFO sqlalchemy.engine.base.Engine {} > 2019-07-03 22:21:46,000 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings > 2019-07-03 22:21:46,000 INFO sqlalchemy.engine.base.Engine {} > 2019-07-03 22:21:46,348 DEBUG sqlalchemy.engine.base.Engine Col ('standard_conforming_strings',) > 2019-07-03 22:21:46,348 DEBUG sqlalchemy.engine.base.Engine Row ('on',) > 2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie)s > 2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine {'movie': "('The Lego Movie'"} > 2019-07-03 22:21:47,375 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review') > 2019-07-03 22:21:47,376 DEBUG sqlalchemy.engine.base.Engine Row ("('The Lego Movie'", Decimal('1'), 'sms', ' > a very cool movie') > 127.0.0.1 - - [03/Jul/2019 22:21:47] "GET /movies/%28%27The%20Lego%20Movie%27%2C%202014%2C%20100%2C%20%27tt1 > 490017%27%2C%20Decimal%28%277.8%27%29%29 HTTP/1.1" 200 - > ```` > Cravan > On 3/7/19, 10:20 PM, "Simon King" <sqlalchemy@googlegroups.com on behalf of si...@simonking.org.uk> wrote: > > Is that screenshot from a review for The Lego Movie? Can you show the > log output when you generate that page? > > On Wed, Jul 3, 2019 at 3:16 PM Cravan <cravan...@gmail.com> wrote: > > > > This is because I have a movie function that throws up any reviews; here it is: > > ```` > > @app.route("/movies/<movie>") > > def movie(movie): > > lol = movie.split(',') > > movie_title = lol[0] > > res = requests.get("http://www.omdbapi.com/", params={"apikey": "c2c76d64", "t": movie_title, "plot": "full"}) > > omdb_data = res.json() > > ratings_list = omdb_data['Ratings'] > > check_for_review_statement = sqlalchemy.text('SELECT * FROM reviews WHERE movie = :movie') > > check_for_review = engine.execute(check_for_review_statement, movie=movie_title).fetchall() > > if res.status_code == 200: > > if len(check_for_review) != 0: > > check_for_review_splitted = str(check_for_review).split(',') > > return render_template("movie_individual.html", movie=lol, check_for_review=check_for_review_splitted, omdb_data=omdb_data, ratings_list=ratings_list) > > else: > > return render_template("movie_no_review.html", movie=lol, omdb_data=omdb_data, ratings_list=ratings_list) > > else: > > if len(check_for_review) != 0: > > check_for_review_splitted = str(check_for_review).split(',') > > return render_template("movie_noomdbdata.html", movie=lol, check_for_review=check_for_review_splitted) > > else: > > return render_template("movie_gotnothing.html", movie=lol, res=res.json()) > > ```` > > And it prints out the review.Here's a pic attached to this email. > > Cravan > > > > > > On 3/7/19, 10:13 PM, "Simon King" <sqlalchemy@googlegroups.com on behalf of si...@simonking.org.uk> wrote: > > > > On Wed, Jul 3, 2019 at 11:54 AM Cravan <cravan...@gmail.com> wrote: > > > > > > Hi Simon, > > > I always turn on debug mode using export FLASK_DEBUG=1 in the shell, but no error pops out, so I have no idea whether that is similar. I tried echo="debug", but didn’t really understand the results, so I am confused as to how to edit my code. I have used both edits as suggested, and this is what was produced: > > > ```` > > > 2019-07-03 18:49:43,100 INFO sqlalchemy.engine.base.Engine select version() > > > 2019-07-03 18:49:43,100 INFO sqlalchemy.engine.base.Engine {} > > > 2019-07-03 18:49:43,764 DEBUG sqlalchemy.engine.base.Engine Col ('version',) > > > 2019-07-03 18:49:43,764 DEBUG sqlalchemy.engine.base.Engine Row ('PostgreSQL 11.4 (Ubuntu 11.4-1.pgdg16.04+1 > > > ) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609, 64-bit',) > > > 2019-07-03 18:49:43,765 INFO sqlalchemy.engine.base.Engine select current_schema() > > > 2019-07-03 18:49:43,765 INFO sqlalchemy.engine.base.Engine {} > > > 2019-07-03 18:49:44,109 DEBUG sqlalchemy.engine.base.Engine Col ('current_schema',) > > > 2019-07-03 18:49:44,110 DEBUG sqlalchemy.engine.base.Engine Row ('public',) > > > 2019-07-03 18:49:44,534 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) > > > AS anon_1 > > > 2019-07-03 18:49:44,534 INFO sqlalchemy.engine.base.Engine {} > > > 2019-07-03 18:49:44,947 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60) > > > ) AS anon_1 > > > 2019-07-03 18:49:44,947 INFO sqlalchemy.engine.base.Engine {} > > > 2019-07-03 18:49:45,353 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings > > > 2019-07-03 18:49:45,353 INFO sqlalchemy.engine.base.Engine {} > > > 2019-07-03 18:49:45,762 DEBUG sqlalchemy.engine.base.Engine Col ('standard_conforming_strings',) > > > 2019-07-03 18:49:45,762 DEBUG sqlalchemy.engine.base.Engine Row ('on',) > > > > Everything above this point is routine stuff that SQLAlchemy does to > > figure out what verison of Postgresql it's talking to and what options > > it is running with. You can safely ignore it. > > > > The next line is the first query that you've run: > > > 2019-07-03 18:49:46,173 INFO sqlalchemy.engine.base.Engine SELECT title, year, "imdbRating" FROM movies WHER > > > E "imdbID" = %(imdb_id)s > > > > These are the parameters that you passed to the query (in this case, > > just the imdb_id): > > > 2019-07-03 18:49:46,173 INFO sqlalchemy.engine.base.Engine {'imdb_id': 'tt1490017'} > > > > The "Col" line tells you the names of the columns in the result set: > > > 2019-07-03 18:49:46,991 DEBUG sqlalchemy.engine.base.Engine Col ('title', 'year', 'imdbRating') > > > > ...and then you will normally see a "Row" line for each row in the > > result. In this case your query returned a single row. The elements of > > the row tuple correspond to the columns from the Col line. > > > 2019-07-03 18:49:46,991 DEBUG sqlalchemy.engine.base.Engine Row ('The Lego Movie', 2014, Decimal('7.8')) > > > > I assume this is from your "print(check_for_api)" statement: > > > ('The Lego Movie', 2014, Decimal('7.8')) > > > > Here's your next query: > > > 2019-07-03 18:49:47,862 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie_title > > > )s > > > > ...and the parameters for that query: > > > 2019-07-03 18:49:47,863 INFO sqlalchemy.engine.base.Engine {'movie_title': 'The Lego Movie'} > > > > The column names in the result set: > > > 2019-07-03 18:49:48,699 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review') > > > > ...but then there are no Row lines, so the query didn't return any > > rows. In other words, there are no rows in the "reviews" table where > > the "movie" column equals "The Lego Movie". > > > > You said that you've confirmed that you've entered the review. How > > have you confirmed that? > > > > Simon > > > > > > > [] > > > {"title": "The Lego Movie", "year": 2014, "imdb_id": "tt1490017", "director": "Phil Lord, Christopher Miller > > > ", "actors": "Will Arnett, Elizabeth Banks, Craig Berry, Alison Brie", "imdb_rating": 7.8, "review_count": 0 > > > , "average_score": "N.A"} > > > 127.0.0.1 - - [03/Jul/2019 18:49:49] "GET /api/tt1490017 HTTP/1.1" 200 - > > > ```` > > > Also, I used three print statements, > > > ```` > > > print(check_for_api) > > > ......#some code(same as before) > > > print(check_for_reviews_unsplitted) > > > ......#some code again(same as before) > > > print (apijson) > > > ```` > > > Thanks, > > > Cravan > > > > > > > > > On 3/7/19, 12:07 AM, "Simon King" <sqlalchemy@googlegroups.com on behalf of si...@simonking.org.uk> wrote: > > > > > > You might want to try turning on debug logging for your SQL statements > > > by passing echo="debug" when creating your engine: > > > > > > https://docs.sqlalchemy.org/en/13/core/engines.html#sqlalchemy.create_engine > > > > > > Amongst other things, this will show the values of parameters that you > > > are passing to the database. > > > > > > I'm not sure what your problem is, but I notice that you are using > > > "SELECT * FROM movies" to load your rows, and then you are assuming > > > that the columns you get back are in a certain order. (You assume that > > > the first column is title, the second is year, and the fifth is > > > imdbrating). It's possible that the columns are coming back in a > > > different order, and so the value you think is the title is actually > > > something else. > > > > > > It would be better to explicitly list the columns you want, something like: > > > > > > SELECT title, year, imdbrating > > > FROM movies > > > WHERE "imdbID" = :imdb_id > > > > > > Alternatively, the rows that you get back from fetchall() actually > > > know which columns they came from, so you could do this: > > > > > > # don't call list() here: > > > check_for_api = check_for_api_unsplitted[0] > > > > > > # use dictionary-style access to get the individual values > > > title = check_for_api["title"] > > > > > > Simon > > > > > > On Tue, Jul 2, 2019 at 3:33 PM Cravan <cravan...@gmail.com> wrote: > > > > > > > > Hi all, > > > > > > > > One of the other tasks for my movie review assignment is that I have to create api access to my website, which should return a json response including a review count. However, I have confirmed that I have entered a review for a specific movie and it prompts out in my html, but apparently when I try to print out the json I get 0 reviews for my review count. Can someone help me troubleshoot? Sorry if it sounds unclear. > > > > > > > > ```` > > > > > > > > @app.route("/api/<imdb_id>") > > > > > > > > def api(imdb_id): > > > > > > > > check_for_api_statement = sqlalchemy.text('SELECT * FROM movies WHERE "imdbID" = :imdb_id') > > > > > > > > check_for_api_unsplitted = engine.execute(check_for_api_statement, imdb_id=imdb_id).fetchall() > > > > > > > > res = requests.get("http://www.omdbapi.com/", params={"apikey": "c2c76d64", "i": imdb_id, "plot": "full"}) > > > > > > > > omdb_data = res.json() > > > > > > > > check_for_api = list(check_for_api_unsplitted[0]) > > > > > > > > title_unsplitted = check_for_api[0] > > > > > > > > title = check_for_api[0] > > > > > > > > year = check_for_api[1] > > > > > > > > imdb_id = imdb_id > > > > > > > > imdbrating = check_for_api[4] > > > > > > > > director = omdb_data['Director'] > > > > > > > > actors = omdb_data['Actors'] > > > > > > > > check_for_reviews_statement = sqlalchemy.text('SELECT * FROM reviews WHERE movie = :movie_title') > > > > > > > > check_for_reviews_unsplitted = engine.execute(check_for_reviews_statement, movie_title=title).fetchall() > > > > > > > > review_count = len(check_for_reviews_unsplitted) > > > > > > > > print(check_for_reviews_unsplitted) #this returns a blank list for some strange reason > > > > > > > > if len(check_for_reviews_unsplitted) != 0: > > > > > > > > check_for_reviews = check_for_reviews_unsplitted.split(',') > > > > > > > > score = float(check_for_reviews[4]) > > > > > > > > average_score = score/review_count > > > > > > > > else: > > > > > > > > average_score = "N.A" > > > > > > > > a = > > > > > > > > {"title":title,"year":year,"imdb_id":imdb_id,"director":director,"actors":actors,"imdb_rating":imdbrating,"review_count":review_count,"average_score":average_score} > > > > > > > > apijson = json.dumps(a, cls=CustomJsonEncoder) > > > > > > > > print (apijson) > > > > > > > > if len(check_for_api) != 0: > > > > > > > > return render_template("api.html", apijson=apijson) > > > > > > > > else: > > > > > > > > return render_template("error2.html", message="No such movie.") > > > > > > > > -- > > > > 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+unsubscr...@googlegroups.com. > > > > To post to this group, send email to sqlalchemy@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/7DB000C8-1EBC-48AF-A24F-202196C743CF%40gmail.com. > > > > For more options, visit https://groups.google.com/d/optout. > > > > > > -- > > > 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+unsubscr...@googlegroups.com. > > > To post to this group, send email to sqlalchemy@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/CAFHwexfVMZTVX6hKDvxgRFG0Wrc%3DhSfwU05XQGm%2B7gsRTtR_Cw%40mail.gmail.com. > > > For more options, visit https://groups.google.com/d/optout. > > > > > > > > > > > > -- > > > 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+unsubscr...@googlegroups.com. > > > To post to this group, send email to sqlalchemy@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/FD18A0DF-70BD-4867-BA8C-A90DD76E24A3%40gmail.com. > > > For more options, visit https://groups.google.com/d/optout. > > > > -- > > 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+unsubscr...@googlegroups.com. > > To post to this group, send email to sqlalchemy@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/CAFHwexc6%2BT%2BzNK%3DR2WPefSgGVkYnC8CyjGkF_ydN3HEFp_ggmg%40mail.gmail.com. > > For more options, visit https://groups.google.com/d/optout. > > > > > > -- > > 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+unsubscr...@googlegroups.com. > > To post to this group, send email to sqlalchemy@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/B6DBDE22-5A75-4DE6-B69C-346E5E597F9E%40gmail.com. > > For more options, visit https://groups.google.com/d/optout. > > -- > 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+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@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/CAFHwexcoCcw1ggw3jcEiVomApD19BaLVKcVsNLU8DSOFa0%2BEzA%40mail.gmail.com. > For more options, visit https://groups.google.com/d/optout. > > > > -- > 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+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@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/2F82992D-69A9-496E-A09D-881F3112EA83%40gmail.com. > For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@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/CAFHwexe2LYymfKO%3DZS1uEFp_0ozJnscghjvfskWw7soah5gq1Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@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/C2E9AF04-9E54-4A08-871D-60B8AE4491C4%40gmail.com. For more options, visit https://groups.google.com/d/optout.
import csv import sys import os import sqlalchemy from sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy import create_engine from flask import Flask, render_template, request, session from flask_sqlalchemy import SQLAlchemy from flask_session import Session engine = create_engine(os.getenv("DATABASE_URL")) # database engine object from SQLAlchemy that manages connections to the database,# DATABASE_URL is an environment variable that indicates where the database lives def main(): f = open("movies.csv","r") reader = csv.reader(f, delimiter=';') for i, row in enumerate(reader): # loop gives each column a name if i == 0: continue if not row: continue print(i,row) title = row[0] year = int(row[1]) runtime = int(row[2]) imdbID = row[3] imdbRating = row[4] insert_statement = sqlalchemy.text('INSERT INTO movies(title, year, runtime, "imdbID", "imdbRating") VALUES (:title, :year, :runtime, :imdbID, :imdbRating)') engine.execute(insert_statement, title=title, year=year, runtime=runtime, imdbID=imdbID, imdbRating=imdbRating) if __name__ == "__main__": main()