Python returns false SQL table

I'm doing an assignment for CS50 where I have to make a website using Python, Flask, SQL, HTML... but I have encountered a very strange problem which I'm stuck on for hours.

The Website is a stock market simulation. You can buy and sell shares with virtual money. I have a SQL table that keep track of who owns how much of what stock. So the table looks like this: SQL Table

The fields "Price" and "Total" are set to None after a purchase and are only filled/refreshed when the user visits the index page, where the user can view all of his/her shares.

stocks = db.execute("SELECT * FROM owners WHERE buyer = (SELECT username FROM users WHERE id = :id)", id = session["user_id"])

    for stock in stocks:

        info = lookup(stock["Symbol"])
        # Insert Price
        db.execute("UPDATE owners SET Price = :price WHERE buyer = (SELECT username FROM users WHERE id = :id) AND Symbol = :symbol", price = info["price"], id = session["user_id"], symbol = stock["Symbol"])

        # Insert Name
        db.execute("UPDATE owners SET Name = :name WHERE buyer = (SELECT username FROM users WHERE id = :id) AND Symbol = :symbol ", name = info["name"], id = session["user_id"], symbol = stock["Symbol"])

        # Insert Total
        db.execute("UPDATE owners SET Total = :total WHERE buyer = (SELECT username FROM users WHERE id = :id) AND Symbol = :symbol", total = usd(stock["Amount"] * info["price"]), id = session["user_id"], symbol = stock["Symbol"])


    return render_template("index.html", money=usd(cash[0]["cash"]), stocks=stocks)

Now the strange part. In the Console Output you can clearly see how the SQL table is being successfully updated.

DEBUG:cs50:UPDATE owners SET Total = '$4,126.71' WHERE buyer = (SELECT username FROM users WHERE id = 18) AND Symbol = 'X'

If I head into phpliteadmin I can also see that table was updated and the values are correct. So the values were inserted and if I print the table I would expect to find those values that I updated.

However as you can see here in the printed table the table's fields are supposedly "None", even though I just updated them and even received conformation, plus im able to see that the values are indeed in the table through phpliteadmin. But still python refuses to print them.

Only after refreshing the page, so basically running all the code again, do the values actually update. It's as if the page tries to displays the values before they are even added to the database, which cant be because im calling the page after updating the values. Even setting a sleep timer between updating the values and loading the page, to prevent the page from trying to load the values before SQL finished updating, doesn't work.

I hope you can help me, thanks.

Read more here:

Content Attribution

This content was originally published by Michael Kessler at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: