0

def buy():

"""Buy shares of stock"""

if request.method == "GET":

    return render_template("buy.html")
else:
    info = lookup(request.form.get("symbol"))

    if not info:
        return apology("invalid symbol", 400)

    shares = int(request.form.get("shares"))

# validate no. shares

    if shares < 0:
        return apology("invalid stock entry", 400)

# validate cash in hand

    cash = db.execute("SELECT cash FROM users where id = :id", id=session["user_id"])
    if not cash or float(cash[0]["cash"]) < info["price"] * shares:
        return apology("insuficient cash")

# decrese the cash in hand

    db.execute("UPDATE users SET cash = cash - :purchase WHERE id = :id",
               id=session["user_id"], purchase=float(info["price"] * shares))

# from here onwards i got the idea from source:endiliey on github

    db.execute("INSERT INTO history(symbol, shares, price, moment, id) VALUES(: symbol, : shares, : price,: moment, : id)", symbol=info["symbol"], shares=shares, price=usd(info["price"]), moment=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), id=session["user_id"])

# update user cash

    db.execute("UPDATE users SET cash = cash - :purchase WHERE id = :id",
               id=session["user_id"], purchase=info["price"] * float(shares))

# Select user shares of that symbol

    user_shares = db.execute("SELECT shares FROM log WHERE id = :id AND symbol=:symbol",
                             id=session["user_id"], symbol=info["symbol"])

# validate shares in hand

    if not user_shares:
        db.execute("INSERT INTO log (name, shares, price, total, symbol, id) VALUES(:name, :shares, :price, :total, :symbol, :id)",
                   name=info["symbol"], shares=shares, price=usd(info["price"]), total=usd(shares * info["price"]), symbol=info["symbol"], id=session["user_id"])

# Else increment the share

    else:
        shares_total = user_shares[0]["shares"] + shares
        db.execute("UPDATE log SET shares=:shares WHERE id=:id AND symbol=:symbol",
                   shares=shares_total, id=session["user_id"], symbol=info["symbol"])

    return redirect("/")

terminal info:

INFO:werkzeug:10.240.0.25 - - [24/Jul/2018 06:05:32] "POST /buy HTTP/1.1" 400 -

INFO:werkzeug:10.240.1.191 - - [24/Jul/2018 06:05:32] "GET /static/styles.css HTTP/1.1" 200 -

DEBUG:cs50:SELECT cash FROM users where id = 13 DEBUG:cs50:UPDATE users SET cash = cash - 13.195 WHERE id = 13

[2018-07-24 06:06:28,380] ERROR in app: Exception on /buy [POST] Traceback (most recent call last): File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 1492, in bindparams existing = new_params[bind.key] KeyError: 'symbol'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app

response = self.full_dispatch_request()

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e)

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb)

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request()

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint

File "/home/ubuntu/workspace/pset7/finance/helpers.py", line 34, in decorated_function return f(*args, **kwargs)

File "/home/ubuntu/workspace/pset7/finance/application.py", line 113, in buy db.execute("INSERT INTO history(symbol, shares, price, moment, id) VALUES(: symbol, : shares, : price,: moment, : id)", symbol=info["symbol"], shares=shares, price=usd(info["price"]), moment=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), id=session["user_id"])

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/cs50/sql.py", line 174, in execute key, value=value, type_=UserDefinedType()))

File "", line 2, in bindparams

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/sqlalchemy/sql/base.py", line 45, in _generative fn(self, *args[1:], **kw)

File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 1496, in bindparams "bound parameter named %r" % bind.key)

sqlalchemy.exc.ArgumentError: This text() construct doesn't define a bound parameter named 'symbol' INFO:werkzeug:10.240.1.50 - - [24/Jul/2018 06:06:28] "POST /buy HTTP/1.1" 500 -

1

Problem is with line

    db.execute("INSERT INTO history(symbol, shares, price, moment, id) VALUES(: symbol, : shares, : price,: moment, : id)", symbol=info["symbol"], shares=shares, price=usd(info["price"]), moment=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), id=session["user_id"])

(which style50 wouldn't like for being too long)

Use :symbol, not : symbol. Same for the other parameters. You used correct spacing for the other database requests, though.

You must log in to answer this question.

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