pset7 is making me crazy. I have huge problems just from the beginning. Register is working, quote is working but Here is another problem in buy:

Buy is writing data to history, reducing cash in users, but when I try to write data to user portfolio i receive this:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near ",": syntax error [SQL: "SELECT * FROM portfolio WHERE user_id= 16, symbol= 'aapl'"]

I don't know what is near ",". Mayby is another stupid mistake like missing letter or something (i'm making a lot of them...).

Here is my buy code:

@app.route("/buy", methods=["GET", "POST"])
def buy():
"""Buy shares of stock."""

if request.method == "POST":

    if not request.form.get("search"):
        return apology("must provide sumbol")

    if not request.form.get("number"):
        return apology("must provide number of shares to buy")

    if float(request.form.get("number")) <= 0:
        return apology("number of shares to buy must be a possitive number")

    quote = lookup(request.form.get("search"))

    if not quote:
        return apology("must provide existing company symbol")

    cash = db.execute("SELECT cash FROM users WHERE id= :id", \

    if quote["price"] * float(request.form.get("number")) > cash[0]["cash"]:
        return apology("not enough cash to buy ordered number of sheres")

    price= lookup(request.form.get("search"))

    db.execute("INSERT INTO history(user_id, name, symbol, shares, price, total) VALUES(:user_id, :name, :symbol, :shares, :price, :total)", \
        user_id= session["user_id"], \
        name= price["name"], \
        symbol= request.form.get("search"), \
        shares= request.form.get("number"), \
        price= price["price"], \
        total= (float(request.form.get("number")) * float(price["price"])))

    db.execute("UPDATE users SET cash = cash - :total WHERE id= :id", \
        total= (float(request.form.get("number")) * float(price["price"])), id= session["user_id"])

    **user_portfolio= db.execute("SELECT * FROM portfolio WHERE user_id= :id, symbol= :search", \
        id= session["user_id"], \
        search= request.form.get("search"))

    if user_portfolio == None:
        db.execute("INSERT INTO portfolio(user_id, name, symbol, shares) VALUES(:user_id, :name, :symbol, :shares)", \
            user_id= session["user_id"], \
            name= price["name"], \
            symbol= request.form.get("search"), \
            shares= request.form.get("number"))
        db.execute("UPDATE portfolio SET shares=shares + :number WHERE user_id= :id, symbol= :symbol", \
            number=request.form.get("number"), \
            id=session["user_id"], \
            symbol= request.form.get("search"))

return render_template("buy.html")**

Ok, I found mistake - after WHERE if there is few conditions there have to be an AND between them

You must log in to answer this question.

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