I’m really stuck on the index part of PSET7….
Flask keeps iterating over the following SELECT query:
dab = db.execute("SELECT SUM(share) FROM portfolio WHERE symbol = :symbol AND user_id = :user_id", symbol=current["symbol"], user_id=session["user_id"])
I used print to check the arguments and the result it saves into “dab“. It seems that the query is doing what it’s supposed to do: Summing all the shares bought by the same user of the same stock:
DEBUG:cs50:SELECT SUM(share) FROM portfolio WHERE symbol = 'NFLX' AND user_id = 3
[{'SUM(share)': 7}]
The sum value is correct. There are 3 corresponding entries in the portfolio table: 2+2+3=7
So how can the value ‚7‘ be ‚NoneType‘?!
The error message in detail:
Code:
@app.route("/")
@login_required
def index():
#all stocks ever bought by user
holdings = db.execute("SELECT symbol FROM portfolio WHERE user_id = :user_id GROUP BY symbol", user_id=session["user_id"])
#current account balance
print(holdings)
balance = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
#create list to store all info relevant for Jinja to display in HTML table
index_lists = []
big_total = 0
#goes this way if user had ever bought any shares
if holdings != []:
#iterate over each stock user had ever bought
for holding in holdings:
#lookup all information on stock and save in dict
current = lookup(holding["symbol"])
print(current)
#query to summarize total amount of shares user currently holds of a share
dab = db.execute("SELECT SUM(share) FROM portfolio WHERE symbol = :symbol AND user_id = :user_id", symbol=current["symbol"], user_id=session["user_id"])
print(dab)
#in case sum of shares is zero (if user bought and sold all shares), jump to iteration for next symbol
if dab[0]["SUM(share)"] == 0:
continue
#create dict for current stock and append it to the list for Jinja.
else:
index_list = {}
index_list["symbol"] = current["symbol"]
index_list["price"] = current["price"]
index_list["shares"] = dab[0]["SUM(share)"]
index_list["total"] = index_list["shares"] * index_list["price"]
holdings.append(index_list)
Would really appreciate some hints. Thank You!!
EDIT: As requested I added additional line to print out for to my code. Here are the results of all print commands:
current:{'name': 'NFLX', 'price': 201.07, 'symbol': 'NFLX'}
holding['symbol']:NFLX
dab:[{'SUM(share)': 7}]
As you can see, none of them is NULL. Also, to make 100% sure that my table doesn't contain any NULL, I dropped it and set it up again setting all fields to "NOT NULL = Yes".
Structure before:
Structure now: (See column "Not NULL")
I wrote the same entries as before into the new table:
Unfortunately it didn't help. The problem still persists.... :(
The only difference is that now Flask stays in debugging mode forever, iterating nonstop over that query line without returning any error message.
current
, but alsoholding["symbol"]
? Maybe you have aNULL
in one record? (the database scheme should be set up to not allowNULL
on that field)holdings.append(index_list)
Maybe you want to use different lists for input and output? Or "augment" the object instead of adding new ones. BTW, you can skip additional requests toportfolio
with something likeSELECT symbol, SUM(share) AS shares FROM portfolio WHERE user_id = :user_id GROUP BY symbol HAVING shares > 0
(HAVING is like WHERE but applied after grouping)