RuntimeError: too many statements at once
I am getting the above error message while trying to implement buy.
@login_required def index(): return apology("Login Required") @app.route("/buy", methods=["GET", "POST"]) @login_required def buy(): """Buy shares of stock.""" if request.method == "GET": return render_template("buyform.html")
else:
if not request.form.get("stock"):
return apology("must provide stock symbol")
elif not request.form.get("nshares"):
return apology("must provide no. of shares you would like to purchase")
elif lookup(request.form.get("stock")) == None:
return apology("Invalid Stock")
else:
username = db.execute("SELECT username FROM users where id = :id1", id1 = session["user_id"])[0]["username"]
sharep = (lookup(request.form.get("stock"))['price'])
nshares = request.form.get("nshares")
cashr = sharep*float(nshares)
cash = db.execute("SELECT cash FROM users where id = :id1", id1 = session["user_id"])[0]["cash"]
name = (lookup(request.form.get("stock"))['name'])
symbol = request.form.get("stock")
if cash >= cashr:
balance = cash - cashr
db.execute("INSERT INTO history (username, name, symbol, sharep, nshares, total, balance, type) VALUES(:username, :name; :symbol, :sharep, :nshares, :total, :balance, 'BOUGHT')", username = username, name = name, symbol = symbol, sharep = sharep, nshares = nshares, total = cashr, balance = balance)
db.execute("UPDATE users SET cash = :cash where id = :id1", cash = balance, id1 = session["user_id"])
rows = db.execute("SELECT * FROM portfolio WHERE username = :username AND symbol = :symbol", username=username, symbol = symbol)
if len(rows) == 1:
existing = db.execute("SELECT shares FROM portfolio where username = :username AND symbol = :symbol", username=username, symbol = symbol)[0]["shares"]
db.execute("UPDATE portfolio SET shares = :shares where username = :username AND symbol = :symbol", shares = existing+nshares, username=username, symbol = symbol)
else:
db.execute("INSERT INTO portfolio (username, name, symbol, shares) VALUES(:username, :name, :symbol, :shares)", username = username, name = name, symbol = symbol, shares = nshares)
return apology ("Bought") #tmeporary
else:
return apology("You can't afford")