1

enter image description hereenter image description here enter image description here

i cannot see any syntax error for several days, so if anything can help is highly appropriated

here is my code :

@app.route("/buy", methods=["GET", "POST"]) @login_required def buy(): """Buy shares of stock""" # User reached route via POST (as by submitting a form via POST) if request.method == "POST": # Ensure symbol was submitted if not request.form.get("symbol"): return apology("must provide symbol", 403) # Ensure shares was submitted shares = int(request.form.get("shares")) if not shares: return apology("must provide shares", 403) if not shares > 0: return apology("must provide positive number of shares", 403)

    # Query database for cash
    rows = db.execute("SELECT cash FROM users WHERE id = :id",
                      id = session["user_id"])
    cash = float(rows[0]["cash"])
    quote = lookup(request.form.get("symbol"))
    price = quote["price"]
    totalprice = price *shares
    company = quote["name"]

    # If enough money, execute buy
    if not cash or cash < totalprice:
        return apology("not enough cash", 403)
    # update tables, transction detail
    db.execute("INSERT INTO transaction (id, company, shares, price, totalprice) VALUES (:id, :company, :shares, :price, :totalprice)",
               id = session["user_id"], company = company, shares = shares, price = price, totalprice = totalprice)
    # update table user for the new cash
    db.execute ("UPDATE users SET cash = cash - :totalprice WHERE id = :id" , id = session["user_id"], totalprice = totalprice)

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

the debug50 error shows: AttributeError: 'RuntimeError' object has no attribute 'name' DEBUG:cs50:SELECT cash FROM users WHERE id = 4 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.iextrading.com DEBUG:urllib3.connectionpool:https://api.iextrading.com:443 "GET /1.0/stock/aapl/quote HTTP/1.1" 200 925 DEBUG:cs50:INSERT INTO transaction (id, company, shares, price, totalprice) VALUES (4, 'Apple Inc.', 1, 209.07, 209.07) ....... RuntimeError: near "transaction": syntax error [SQL: "INSERT INTO transaction (id, company, shares, price, totalprice) VALUES (4, 'Apple Inc.', 1, 209.07, 209.07)"]

i think the value showed here is exact as what i typed in and suppose to be. so in insert line must have syntax error. and if i put this line into SQL in the data file, it does not work as well.

  • If the SQL fails directly, are you sure that you have the correct table name and the correct column names and types? – curiouskiwi Aug 7 '18 at 5:18
  • here is my table, i think it is right – Yao Lu Aug 7 '18 at 5:29
1

I would expect you to have an autoincrementing id field that is the primary key (so each transaction has an id) and then a user_id field that holds the user_id for the transaction (that is not unique). This way your table can handle multiple users.

Your insert is failing because you are trying to set the primary key.

| improve this answer | |
  • so i changed the transaction data base and added what you mentioned and i left my code the same as before.However, it still does not work. anything more specific i can do pls? thanks. sorry for taking this long to revisit. – Yao Lu Aug 18 '18 at 3:18
  • did you change your INSERT to use the new user_id column that you added? – curiouskiwi Aug 18 '18 at 3:22
  • # update tables, transction detail db.execute("INSERT INTO transaction (company, shares, price, totalprice, id) VALUES (:company, :shares, :price, :totalprice, :id)", company = company, shares = shares, price = price, totalprice = totalprice,id = session["user_id"]) i think i did, because i changed the primary of this table called tradedid, and keep the userid still called id, similar as users table, which is not the primary unique key – Yao Lu Aug 18 '18 at 3:24
  • If you run that SQL directly, does it work? Or do you see errors? – curiouskiwi Aug 18 '18 at 3:27
  • INSERT INTO transaction (company, shares, price, totalprice, id) VALUES ('Apple Inc.', 1, 217.58, 217.58, 4)"]-------this is the correct value i want to insert and i pull it from the error area. if i put in SQL directly, it shows " NSERT INTO transaction (company, shares, price, totalprice, id) VALUES ('Apple Inc.', 1, 217.58, 217.58, 4)"] ERROR: near "transaction": syntax error" – Yao Lu Aug 18 '18 at 3:32

You must log in to answer this question.

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