0

Implementing the Buy route and running into difficulty with updating the DB...I've tested the rest of my code which all works, but whenever I use the two db.execute lines at the end of the code, I get error 500... I've tried commenting out each individually as well to see where the problem stem from, but seems that both give a problem

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "POST":

    # check if stock exists, share input is valid
    stock = lookup(request.form.get("symbol"))

    try:
        shares = int(request.form.get("shares"))
    except:
        return apology("shares must be a number")

    if not stock:
        return apology("Stock not found")

    if shares < 1:
        return apology("Sorry, can't buy less than one share")

    # check if user has the $$
    rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
    funds = rows[0]["cash"]

    price = stock["price"]
    tx_value = shares * price

    if tx_value > funds:
        return apology("Sorry, insufficient cash to perform this transaction")


    # insert tx into transactions and update user cash

    db.execute("UPDATE users SET cash = cash - :tx_value WHERE id = user_id", tx_value=tx_value, user_id=session["user_id"])
    db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES(:user_id, :symbol, :shares, :price)", user_id=session["user_id"], symbol=request.form.get("symbol"), shares=shares, price=price)**

    return redirect("/")

else:
    return render_template("buy.html")
1

The flask error log (in the console where flask run) should give you all the info you need to troubleshoot and debug.

Problem with the first (hover to see spoiler):

This db.execute("UPDATE users SET cash = cash - :tx_value WHERE id = user_id", tx_value=tx_value, user_id=session["user_id"])
is sending two variables, but only has one binding (looks like a typo: missing :)

Problem with the second:

This db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES(:user_id, :symbol, :shares, :price)", user_id=session["user_id"], symbol=request.form.get("symbol"), shares=shares, price=price)**
is a python syntax error.

| improve this answer | |
  • Thanks, 100% on the first one. Regarding 2nd, looked into flask error log, and I'm getting back: "RuntimeError: table transactions has no column named user_id". However, I actually have a column named user_id in my transactions table. Any ideas why it wouldn't be seeing it? – yehoshua zlotogorski Mar 19 '19 at 13:15
  • The simplest explanation would be a typo in the column name. I don't have eyes-on, which you do, so I'd have to side with the computer in this disagreement :) – DinoCoderSaurus Mar 19 '19 at 13:59
  • Actually just found what I guess is the problem - column names are named 'user_id' in the database, so have to actually call it in the SQL ''user_id'' which is a mess... – yehoshua zlotogorski Mar 19 '19 at 14:01
  • I am having a hard time imagining the conditions under which you would be required to " the column name. Something else is afoot. – DinoCoderSaurus Mar 19 '19 at 14:15
  • Honestly don't know. Removed the ' ' around the names of the columns and that solved it. – yehoshua zlotogorski Mar 20 '19 at 16:48

You must log in to answer this question.

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