1
@app.route("/buy", methods=["GET", "POST"])

@login_required def buy(): """Buy shares of stock""" if request.method == "POST": symbol = request.form.get("symbol").upper() item = lookup("symbol")

    if not symbol:
        return apology("MISSING SYMBOL")

    elif not item:
        return apology("MISSING SYMBOL")
    # Check share is numeric data type
    try:
        shares = int(request.form.get("shares"))
    except:
        return apology("INVALID SHARES")
    # Check shares is positive number
    if not (shares >= 0):
        return apology("INVALID SHARES")

    user_id = session["user_id"]
    cash = db.execute("SELECT * FROM users WHERE id = ?",user_id)[0]["cash"]

    item_name = item["name"]
    item_price = item["price"]
    total_price = item_price * shares


    if cash > total_price:
        return apology("not enough cash")
    db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_price, user_id)
    db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_price, user_id)
    db.execute("INSERT INTO transactions(user_id, name, shares, price, type, symbol) VALUES(?, ?, ?, ?, ?, ?,)",user_id, item_name, shares, item_price, buy, symbol)


    return redirect('/')
else:
    return render_template("buy.html")


after running my code i get this error

Traceback (most recent call last):
     File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2447, in wsgi_app
     response = self.full_dispatch_request()
     File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
     File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
     File "/usr/local/lib/python3.10/site-packages/flask/_compat.py", line 39, in reraise
raise value
     File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
     File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
     File "/workspaces/88515181/finance/helpers.py", line 34, in decorated_function
return f(*args, **kwargs)
     File "/workspaces/88515181/finance/app.py", line 96, in buy
db.execute("INSERT INTO transactions(user_id, name, shares, price, type, symbol) VALUES(?, ?, ?, ?, ?, ?,)",user_id, item_name, shares, item_price, buy, symbol)
     File "/usr/local/lib/python3.10/site-packages/cs50/sql.py", line 27, in decorator
return f(*args, **kwargs)
     File "/usr/local/lib/python3.10/site-packages/cs50/sql.py", line 189, in execute
_args = ", ".join([str(self._escape(arg)) for arg in args])
     File "/usr/local/lib/python3.10/site-packages/cs50/sql.py", line 189, in <listcomp>
_args = ", ".join([str(self._escape(arg)) for arg in args])
     File "/usr/local/lib/python3.10/site-packages/cs50/sql.py", line 488, in _escape
return __escape(value)
     File "/usr/local/lib/python3.10/site-packages/cs50/sql.py", line 482, in __escape
raise RuntimeError("unsupported value: {}".format(value))
     RuntimeError: unsupported value: <function buy at 0x7f1c380cfd90>

1 Answer 1

2

buy in this sentence

db.execute("INSERT INTO transactions(user_id, name, shares, price, type, symbol) VALUES(?, ?, ?, ?, ?, ?,)",user_id, item_name, shares, item_price, buy, symbol)

refers to the function buy. Perhaps you meant the string literal 'buy'.

You must log in to answer this question.

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