0

I am currently working on the finance assignment however I am facing some difficulties updating the cash variable into my users database. All other database updates work fine but when it tries to update the cash I get the following error:

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

This is my code, I hope anyone can help me with this problem. Thanks in advance :)

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock"""

if request.method == "POST":

    # Get how much shares user wants to sell
    quantity = float(request.form.get("quantity"))

    # Valid quantity check
    if not quantity or quantity <= 0:
        return apology("please enter a valid amount")

    # Fetch stock information
    try:
        stock = lookup(request.form.get("symbol"))
    except:
        return apology("Could not fetch stock data")

    # Get stock from db
    container = db.execute("SELECT * FROM transactions WHERE id = :id AND stock = :stock",
                id=session["user_id"], stock=stock["symbol"])

    # Number of shares counter
    share_count = 0

    # Count total number of shares
    for i in range(len(container)):
        share_count += container[i]["shares"]

    # Update shares quantity
    new_shares = share_count - quantity

    # Make sure transaction is valid,
    if new_shares < 0:
        return apology("Invalid transaction, not enough shares")

    # Delete old values from db
    db.execute("DELETE FROM transactions WHERE id = :id and stock = :stock",
    id = session["user_id"], stock=stock["symbol"])

    # Create new db entry with updated stock quantity at old prices
    db.execute("INSERT INTO transactions (id, stock, shares, price, value) VALUES (:id, :stock, :shares, :price, :value)", \
                id = session["user_id"], \
                stock = stock['symbol'], \
                shares = new_shares, \
                price = container[0]['price'], \
                value = container[0]['price'] * new_shares)

    # Get that cash
    cash = db.execute("SELECT cash FROM users WHERE id = :id")
    difference = stock['price'] * quantity

    # Update cash in user table
    db.execute("UPDATE users SET cash = cash + :difference  WHERE id = :id", id = session["user_id"], difference=difference)


    return render_template("index.html")

else:

    # Get stocks for user_id
    container = db.execute("SELECT stock FROM transactions WHERE id = :id", id=session["user_id"])

    # Container for stocks
    stocks = []

    # Append unique values to stocks
    for i in range(len(container)):
        stock = container[i]['stock']
        if stock not in stocks:
            stocks.append(stock)

    return render_template("sell.html", stocks=stocks)
2
  • It's a problem when it tries to get the cash here: cash = db.execute("SELECT cash FROM users WHERE id = :id") since there's a parameter missing in the execute call. May 8, 2019 at 14:27
  • Thank you so much! May 8, 2019 at 15:05

1 Answer 1

1

Check line cash = db.execute("SELECT cash FROM users WHERE id = :id")!

You must log in to answer this question.

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