0

(1) "buy" code in application.py:

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

    if request.method == "POST":
        
        shares = int(request.form.get("shares"))
        symbol = lookup(request.form.get("symbol"))

        if symbol == None:
            return apology("no symbol entered")

        if shares <= 0:
            return apology("no shares entered")
        
        rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])
        
        cash_owned = rows[0]["cash"]
        per_share_price = symbol["price"]
        
        total_cost = shares * per_share_price
        if total_cost > cash_owned:
            return apology("not enough cash")
            
        db.execute("UPDATE users SET cash = cash - :price WHERE id = :user_id", user_id = session['user_id'], price = per_share_price)
        db.execute("INSERT INTO transactions (user_id, symbol, shares, per_share_price) VALUES(:user_id, :symbol, :shares, :price)", 
                    user_id = session["user_id"], symbol = symbol, shares = shares, price = per_share_price)

        return render_template("TODO")

Also for reference, I created an SQL table names "transactions" with the parameters shown in the code above

Runtime error message on inputting AAPL in the symbol form:

RuntimeError: unsupported value: {'name': 'Apple Inc', 'price': 125.86, 'symbol': 'AAPL'}

Complete output in terminal:

DEBUG: SELECT cash FROM users WHERE id = 1
DEBUG: UPDATE users SET cash = cash - 125.86 WHERE id = 1
ERROR: Exception on /buy [POST]

Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)

File "/home/ubuntu/finance/helpers.py", line 34, in decorated_function
return f(*args, **kwargs)
File "/home/ubuntu/finance/application.py", line 79, in buy
db.execute("INSERT INTO transactions (user_id, symbol, shares, per_share_price) VALUES(:user_id, :symbol, :shares, :price)",

File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 21, in decorator
return f(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 216, in execute
tokens[index] = self._escape(kwargs[name])
File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 474, in _escape
return __escape(value)
File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 468, in __escape
raise RuntimeError("unsupported value: {}".format(value))

RuntimeError: unsupported value: {'name': 'Apple Inc', 'price': 125.86, 'symbol': 'AAPL'}
0

symbol is the dictionary returned from lookup here symbol = lookup(request.form.get("symbol")). That is what it is trying insert into the the symbol column in the transactions table. Did you mean the symbol key from the symbol dictionary?

1
  • such a simple silly mistake I somehow missed for so long, Thanks, the second I read this the realization hit me like a bus.
    – starbvuks
    Feb 25 at 5:37

You must log in to answer this question.

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