I'm stuck on pset8 c$50 finance /buy: Form validation works properly but when i try to execute the INSERT INTO transactions table i get the following error:

RuntimeError: (sqlite3.OperationalError) near "'A'": syntax error
[SQL: INSERT INTO transactions (buyer, symbol, price, shares, total)
VALUES(8 'A' 72.16 1 72.16)]

my "transactions" table query is as follows:

CREATE TABLE 'transactions'
'buyer' integer NOT NULL,
'symbol' text NOT NULL,
'price' real NOT NULL,
 'shares' INTEGER NOT NULL ,
 'transacted' datetime NOT NULL DEFAULT 'datetime(''now'')')

i'll add that i tried to implement the 'transacted' field in a various ways(with defaults or without etc..) even though it doesn't seem to be the problem

here's the current code i wrote:

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

# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":    

    # Form validation
    if not request.form.get("symbol"):
        return apology("Missing symbol")
    elif not request.form.get("shares"):
        return apology("Missing shares")
    elif int(request.form.get("shares")) <= 0:
        return apology("Invalid Shares")
    elif not lookup(request.form.get("symbol")):
        return apology("Invalid Symbol")

    # Review user's cash
    cash = db.execute("SELECT cash FROM users WHERE id= :id", \

    # Ensure user can afford the transaction
    stock = lookup(request.form.get("symbol"))
    shares = int(request.form.get("shares"))
    total = stock["price"] * shares

    if cash[0]["cash"] < total:
        return apology("Can't Afford")

    # Confirm transaction, update user's balance
    transaction = db.execute("INSERT INTO transactions (buyer, symbol, price, shares, total) VALUES(:buyer :symbol :price :shares :total)", \
    buyer=session["user_id"], symbol=stock["symbol"], price=stock["price"], shares=shares, total=total)

    cash = db.execute("UPDATE users SET cash = :cash WHERE id = :id", \
    cash=cash - total, id=session["user_id"])

    return redirect("/")

# User reached route via GET (as by clicking a link or via redirect)
    return render_template("buy.html")

thanks in advance for your help!


yes, i got it. it was just me not aware that i have to add comma between each VALUE. finally done with /buy!

You must log in to answer this question.

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