0

There is a syntax error when I try to insert the buy information into my trade table. Here is my code. The issue is at the long db.execute line. Any guidance would be great. Thanks.

def buy():
    """Buy shares of stock"""
    if request.method == "POST":
        # Ensure ticker was submitted
        if not request.form.get("symbol"):
            return apology("must input stock symbol")

        # Get symbol and make sure it exists
        symbol=request.form.get("symbol")
        if not lookup("symbol"):
            return apology("symbol does not exist")

        # Ensure shares entered greater than zero
        shares=int(request.form.get("shares"))
        if shares < 0 or shares == NULL:
            return apology("must enter number of shares to buy")

        # set up dict to store purchase info: name, price, symbol
        purc = {}
        purc = lookup("symbol")

        # Dollars needed for purchase
        pcash=shares * purc[price]

        # Is there enough cash in account to purchase
        ecash = db.execute("SELECT cash FROM users WHERE id = ?", session["user-id"])
        if pcash >= ecash:
            return apology("Not enough cash to buy that amount of stock")

        # Record buy in trades.db for user-id
        x=datetime.datetime.now()
        db.execute("INSERT INTO trades (datetime, symbol, shares, price, total, id) VALUES(?, ?, ?, ?, ?, ?)" x, symbol, shares, purc[price], pcash, session["user-id"])


        # Adjust cash to reflect purchase
        db.execute("UPDATE users SET cash=? WHERE id=session["user-id"]", ecash-pcash)

        # Redirect user to home page
        return redirect("/")


     # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("buy.html")
3
  • could you add the terminal output displaying the error – Vsjain Mar 24 at 8:22
  • there's a missing comma between the query and the parameters; ie before the x. – DinoCoderSaurus Mar 24 at 10:15
  • Thank you! It was so blatant once you pointed it out. If you knew how many times I missed it before posting........ – JerryCinc Mar 24 at 21:08

You must log in to answer this question.

Browse other questions tagged .