In my buy function I cant INSERT or UPDATE data in SQL.

RuntimeError: (sqlite3.OperationalError) near "ON": syntax error [SQL: INSERT INTO buy (share_name, share_symbol, share_price, shares, id) VALUES ('Netflix, Inc.', 'NFLX', 275.3, '3', 4) ON DUPLICATE KEY UPDATE shares = shares + VALUES (shares)]

this is my code:

def buy():
"""Buy shares of stock"""
if request.method == "POST":
    # check user to feel forms html
    if not request.form.get("shares"):
        return apology("You must chuse at least 1 share")
    if not request.form.get("symbol"):
        return apology("You must to put symbol")
    # get info about shears and their qwantity
    rows = lookup(request.form.get("symbol"))
    # if there is no shears
    if not rows:
        return apology ("Incorect symbol")
        #checking how much money have the user
        #list users money
        list_money=db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
        #dic users money
        #geting price for shares
        if money < price:
            return apology ("you have not enought money")
            db.execute("UPDATE users SAT cash = :cash WHERE id = :id", cash=money, id=session['user_id'])

            db.execute("INSERT INTO buy (share_name, share_symbol, share_price, shares, id) VALUES (:share_name, :share_symbol, :share_price, :shares, :id) ON DUPLICATE KEY UPDATE shares = shares + VALUES (shares) ", share_name=rows['name'], share_symbol=rows['symbol'], share_price=rows['price'], shares=shares, id=session["user_id"])
            return render_template("/ubuy.html", rows = rows, shares = shares, price = price)
    return render_template("/buy.html")

Update also do not work with sintax error.

1 Answer 1


on duplicate key is not supported in sqlite3

  • but why also do not work Insert and give syntax error? I try to update without duplicate key ... still the same problem(
    – Tory
    Oct 21, 2019 at 12:26

You must log in to answer this question.

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