0

Here is my python code.

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "GET":
        return render_template("sell.html")

    if request.method == "POST":

        stock_to_sell = request.form.get("stock_to_sell")
        quantity_to_sell = int(request.form.get("quantity_to_sell")[0])
        quantity_owned = int((db.execute ("SELECT quantity FROM user_stocks WHERE user_id = ?"), session['user_id'])[0])

        if not stock_to_sell:
            return apology("Please enter the symbol of the stock to sell.")

        elif not quantity_to_sell:
            return apology("Please enter the quantity of stock to sell.")

        elif quantity_to_sell > quantity_owned:
            return apology("Insufficient stocks to sell.")

        else:
            quantity_owned = quantity_owned - quantity_to_sell
            db.execute ("INSERT INTO user_stocks (quantity) VALUES (?)", quantity_owned)

            cash = cash + quantity_to_sell * db.execute ("SELECT cash FROM users WHERE id = ?", session["user_id"])
            db.execute ("INSERT INTO users (cash) VALUES (?)", cash)

            action = "sell"
            db.execute("INSERT INTO history (stock, quantity, time, action) VALUES (?, ?, ?, ?) WHERE user_id = ?", stock, quantity, datetime('now', 'localtime'), action, session["user_id"])

            return render_template("sell.html")

The error in the terminal was: RuntimeError: more placeholders (?) than values ()

1

Assume it is this line that produces error:

quantity_owned = int((db.execute ("SELECT quantity FROM user_stocks WHERE user_id = ?"), session['user_id'])[0])

There is a ) in the wrong place. session['user_id'] is an argument to the execute call.

Spoiler: There are other problems ahead

- This INSERT INTO user_stocks (quantity) VALUES (?)" will insert a row. Which user? Which stock?
- INSERT INTO users (cash) VALUES (?). Did you mean UPDATE?
- WHERE is not a valid phrase in an INSERT query.

You must log in to answer this question.

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