0

So I´m currently working in the /index route and noticed it doesn't update the values when I buy something. After looking at the finance.db, I realized it doesn't update the shares or ptice at all with my /buy route when I have an already existing row wuth the same username. I can't see what´s wrong so any help is appreciated

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""
    #confirming user submitted the form
    if request.method == "POST":
        #finding current user id
        id = int(session["user_id"])
        #checking all info is present
        if not request.form.get("symbol") or not request.form.get("shares"):
            return apology("Must fill out all fields", code = 403)
        #Chcking shares is an integer
        try:
            int(request.form.get("shares"))
        except ValueError:
            return apology("Shares must be an integer", code = 403)
        symbol = request.form.get("symbol")
        #definig variables
        if not lookup(symbol):
            return apology("Symbol doesn't exist", code = 403)
        shares = int(request.form.get("shares"))
        #checking shares is positive
        if not shares > 0:
             return apology("Shares must be a positive integer", code = 403)
        #getting user cash, username
        raw_cash = (db.execute("SELECT cash FROM users WHERE id = :id", id = id))
        cash = float(raw_cash[0]['cash'])
        raw_username = (db.execute("SELECT username FROM users WHERE id = :id", id = id))
        username = str(raw_username[0]['username'])
        stock_price = lookup(symbol)['price']
        #checking user has sufficient fonds
        if stock_price * shares > cash:
            return apology("insufficient funds to buy shares", code = 403)
        else:
            #updating cash value
            cash -= lookup(symbol)["price"] * shares
            db.execute("UPDATE users SET cash = :cash WHERE id = :id", cash = cash, id = id)
            #getting the users share symbols
            rows = db.execute("SELECT * FROM portfolio WHERE username =:username AND symbol = :symbol", username=username, symbol=symbol)
            #checking wether user has shares from this symbol
            if len(rows) != 1:
            #if there is none insert current values
                price = stock_price * shares
                db.execute("INSERT INTO portfolio (username, symbol, shares, price) VALUES (:username, :symbol, :shares, :price)", username=username, symbol=symbol, shares=shares, price=price)
                return redirect("/")
            else:
                #adding share amounts
                raw_old_shares = db.execute("SELECT shares FROM portfolio WHERE username =:username", username=username)
                old_shares = int(raw_old_shares[0]['shares'])
                shares += old_shares
                new_price = stock_price * shares
                db.execute("UPDATE portfolio SET shares = :shares AND price = :price WHERE username =:username and symbol=:symbol", shares=shares, price=new_price, username=username, symbol=symbol)
                return redirect("/")

elif request.method == "GET":
    return render_template("buy.html")
0

I figured it out by myself. Had an error in the update and the AND had to be a ,

You must log in to answer this question.

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