When I try to update the users portfolio I receive a runtime error from passing in 'when' as date('now'). Please can someone tell what I should be doing. Here is part of my buy function code.

   # calculate total price
    total = quote["price"]*amount
    cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])[0]["cash"]

    # check if user has sufficient cash and update portfolio
    if(cash >= total):
        db.execute("INSERT INTO portfolio (id, price, number, symbol, when) VALUES (:id, :price, :number, :symbol, date('now'))", id = session["user_id"], price = quote["price"], number = amount, symbol = quote["symbol"])
        db.execute("UPDATE users SETS cash = cash - :price WHERE id = :id", price = amount, id = session["user_id"])
        return apology("insufficient cash for chosen stock")

    # Redirect user to home page
    return redirect("/")
  • What's the exact error message? Maybe also post the table schema (in sqlite3 finance.db, do .schema portfolio). I think calling your first field id is a bad choice, as this is an id from table users, so user_id would be more appropriate. – Blauelf Jul 10 '18 at 15:29
  • I meant portfolio.id as being misleading, users.id is fine. – Blauelf Jul 10 '18 at 15:34

when is a keyword in sqlite. You'll have to escape it, single-quotes I think eg 'when'(or maybe backticks). Or you could create the table again and rename the column.

You must log in to answer this question.

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