1

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:

enter image description here

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:

enter image description here

Structure now: (See column "Not NULL")

enter image description here

I wrote the same entries as before into the new table:

enter image description here

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.

3
  • Would you mind printing not just current, but also holding["symbol"]? Maybe you have a NULL in one record? (the database scheme should be set up to not allow NULL on that field)
    – Blauelf
    Jan 2, 2018 at 10:52
  • Thank you so much for your reply, Blauelf. As per your request, I added the additional print statement and build up a new portfolio table to not allow any NULL. Unfortunately it didn't help. :( Please see screenshots above for further clarification.
    – Faye_108
    Jan 2, 2018 at 22:51
  • Umm... just realized this one: 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 to portfolio with something like SELECT 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)
    – Blauelf
    Jan 3, 2018 at 10:57

2 Answers 2

0

@blauelf thank you so much! It works perfectly. :) I must use the list „index_lists“ instead of „holdings“ which I had created before specifically for this purpose. In the future I really should adopt the habit to use better distinguishable variable names to avoid such mistakes.

Only downside is that the part with the SQL query for the index function probably takes longer than it should (5-7secs to load), but it works fine.

Now after I checked to make sure that the program runs properly, I migrated* everything from the 2017 to the 2018 version.

I made some checks and the programs works as fine as the old version.

I'm not sure whether I should post this problem as a completely new question, but here we go: Unfortunately there is a check50 for this Pset in the 2018 version, and unfortunately my program gets stuck during the checking process. After a long checking time I get the following error message in the terminal:

„check50 is taking longer than normal!
See https://cs50.me/checks/17941b280bd67ad63df8aded59b05d8d3cddf46b for more 
detail.“

According to the content of the URL above I should contact the CS50 sys admins. I did so, but no answer so far. Is there someone who could guide me into the direction on how to solve this problem? Much appreciated!

*By migration I mean: - renaming old folder to pset7_2017

  • downloading 2018 distribution code and creating „pset7“ folder

  • only adding (not replacing) html-files in the templates folder (adding buy/history/index/quote/quoted/register/sell.html, but not touching apology/layout/login.html)

  • not replacing the new application.py, but only copy’n pasting functions/code lines into the new application.py which I wrote myself (i.e. buy/sell/history/index, but not touching login/logout/layout/apology).

  • I use „generate_password_hash“ instead of the old „pwd_context“

  • I replaced the new finance.db file by the old one, but made sure, but made sure beforehand that the default settings of both versions are the same.

  • In accordance to the updated specifications I wrote additional code line to implement password confirmation in the register function.

  • Of course I use the new helpers.py and styles.css.

0

Ok, got my answer from CS50: They instructed me to remove and print statements in my code (those that I had previously implemented for testing.) Now check50 runs smoothly.

You must log in to answer this question.

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