0

I am trying to create the table in pset9 finance to display what stocks the user has. My current attempt tries to do this by summing up the number of stocks in past transactions the user has made.

I keep getting syntax errors on the line number = db.execute("SELECT SUM(size) AS "Total" FROM history WHERE id = ? AND symbol = ?", session["user_id"], symbol)

Full code for this route:

@login_required
def index():
    """Show portfolio of stocks"""

    stocks_owned = db.execute("SELECT DISTINCT symbol FROM history WHERE id = ?", session["user_id"])
    #stocks_owned is a list of dictionaries

    dictionary = { }

    for n in stocks_owned:
        symbol = stocks_owned[n]["symbol"]
        number = db.execute("SELECT SUM(size) AS "Total" FROM history WHERE id = ? AND symbol = ?", session["user_id"], symbol)
        stock_count = number[0]["Total"]
        dictionary[symbol] = stock_count

    return render_template("index.html", dictionary = dictionary)```
1

The problem is the double double-quotes. The first phrase is: "SELECT SUM(size) AS " the second phrase is: " FROM history WHERE id = ? AND symbol = ?" and Total is in no-man's land. It is not necessary to enclose table/column aliases with delimiters, unless they contain special characters. If that were the case, Total would need a different delimiter (eg single quote ').

You must log in to answer this question.

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