0

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"))
    shares=request.form.get("shares")
    # if there is no shears
    if not rows:
        return apology ("Incorect symbol")
    else:
        #checking how much money have the user
        price=rows['price']*float(shares)
        #list users money
        list_money=db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
        #dic users money
        dic_money=list_money[0]
        money=dic_money['cash']
        #geting price for shares
        if money < price:
            return apology ("you have not enought money")
        else:
            money=money-price
            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)
else:
    return render_template("/buy.html")

Update also do not work with sintax error.

0

on duplicate key is not supported in sqlite3

1
  • 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 '19 at 12:26

You must log in to answer this question.

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