0

Stock holdings are not printing on my index webpage. "cash" prints correctly. I think the SELECT on my finance.activity table is somehow coming up blank. I am not receiving any errors, and when I include 'print(rows)' the console prints "[]". The ID reflected in the console is correct ("ID = 6") The SELECT for 'cash' from finance.users works fine and prints correctly on the webpage.

To keep things simple, I deleted all rows in the activity table, except for one. Also, I have taken a stab at setting up the "rows" dict, but I am unable to debug that so far since I do not know what data is being selected in the first place. That is, I expect there are issues there, but do not see how to fix yet until I get past the blank SELECT (if that is the problem).

Any help would be greatly appreciated.

index route within application.py

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""
    # Select data
    rows = db.execute("SELECT ticker, SUM(shares) FROM activity WHERE id = :id GROUP BY ticker", id=session["user_id"])
    print(rows)
    for row in rows:
        ticker = (row["ticker"])
        shares = (row["shares"])
        quote = lookup(ticker)
        row["name"] = (quote["name"])
        row["price"] = usd(quote["price"])
        row["value"] = usd(quote["price"] * shares)

    # Grand total value TODO

    value = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
    cash = usd(value[0]["cash"])

    return render_template("index.html", rows=rows, cash=cash)

Console Results:

INFO:werkzeug:192.168.244.110 - - [14/Sep/2019 23:59:02] "GET / HTTP/1.0" 302 -
INFO:werkzeug:192.168.167.154 - - [14/Sep/2019 23:59:03] "GET /login HTTP/1.0" 200 -
INFO:werkzeug:192.168.167.154 - - [14/Sep/2019 23:59:03] "GET /static/styles.css HTTP/1.0" 200 -
DEBUG:cs50:SELECT * FROM users WHERE username = 'mdisa'
INFO:werkzeug:192.168.167.154 - - [14/Sep/2019 23:59:06] "POST /login HTTP/1.0" 302 -
DEBUG:cs50:SELECT ticker, SUM(shares) FROM activity WHERE id = 6 GROUP BY ticker
[]
DEBUG:cs50:SELECT cash FROM users WHERE id = 6
INFO:werkzeug:192.168.167.154 - - [14/Sep/2019 23:59:06] "GET / HTTP/1.0" 200 -
INFO:werkzeug:192.168.167.154 - - [14/Sep/2019 23:59:06] "GET /static/styles.css HTTP/1.0" 200 -
1

SELECT ticker, SUM(shares) FROM activity WHERE id = 6 GROUP BY ticker

Are you sure you want to get rows with id = 6? I'd expect that you want user_id = 6.

1
  • Yep, I missed the obvious. Changed to 'WHERE user_id = :id' and I am back in business. Thank you so much for your help. – mdisandro2 Sep 15 '19 at 6:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .