0

My portfolio table tracks correctly the number of shares bought, however I encounter issues when I also try to update the cash that's left after this "purchase" in the users table.

I have defined the different variables as follows (I'm posting here only the relevant parts of the code):

 //obtain number of shares from the user input
    number = request.form.get("shares")

//obtain the price of the stock chosen by the user and calculate the total amount to be paid based on the number of stocks bought and their price

    rows = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
    price =quote.get("price")
    purchase = price * number

As mentioned above, the portfolio table updates the input correctly. However, the line that is supposed to update the cash left in the users table does not work:

db.execute=("UPDATE users SET cash= cash -:purchase WHERE id=:id", id = session["user_id"])

It gives me the following error message:

 sqlalchemy.exc.CompileError: Bind parameter 'purchase' without a renderable value not allowed here.

Any ideas what the issue(s) might be? I'm confused what is meant by "renderable" value in this case.

Thank you all in advance!

1 Answer 1

1

The problem is here db.execute=("UPDATE users SET cash= cash -:purchase WHERE id=:id", id = session["user_id"]). You are binding :purchase but you never set it to anything.

2
  • Thanks a lot, solved! So simple and yet, I missed it :) What is the difference between assigning a value to a variable (as I did in the beginning of the code) and "setting" it to something when I want to update the users table? I'm probably confusing the two, as I actually (wrongly) thought that if I have already assigned a value to "purchase" beforehand, I don't need to take any action later on. Now I added "purchase=purchase" in the problematic line of code, following your advice, and it worked.
    – GiP
    Commented Apr 1, 2018 at 23:02
  • The difference is: that's the syntax of the db.execute, full stop. Commented Apr 1, 2018 at 23:51

You must log in to answer this question.

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