0

Right now I'm attempting to insert some information from the user's transaction into my table with this code:

# Record transaction
        db.execute("INSERT INTO trading buyer = :buyer, ticker = :ticker, price = :price, date = :date ", buyer=session["user_id"], ticker=buy["symbol"], price=buy["price"], date=now)

Now the really odd part is that on the terminal, it shows all the values being correctly inserted but I'm still getting an error nonetheless, and the table isn't actually updating.

Error

I'm not really sure how to read this. How can the syntax be wrong if all the values are being correctly inserted?

Some related code:

        shares = request.form.get("shares")
    result = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
    shares = float(shares) # Convert shares to float for math
    if result[0]["cash"] >= buy["price"] * shares: # Check if user has enough cash
        # Updates user's funds
        db.execute("UPDATE users SET cash = :cash WHERE id = :id", id=session["user_id"], cash=result[0]["cash"] - buy["price"] * shares)
        now = datetime.datetime.now()

Additional errors: File "/home/ubuntu/workspace/pset7/finance/helpers.py", line 34, in decorated_function return f(*args, **kwargs) File "/home/ubuntu/workspace/pset7/finance/application.py", line 91, in buy date=now) File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/cs50/sql.py", line 224, in execute raise e RuntimeError: near "'AAPL'": syntax error [SQL: "INSERT INTO trading (\n buyer,\n ticker,\n price,\n date)\n VALUES\n (\n 4\n 'AAPL'\n 186.05\n '2018-05-09')"] (Background on this error at: http://sqlalche.me/e/e3q8)

1

INSERT INTO does not use =. See for example http://www.sqlitetutorial.net/sqlite-insert/

SQLite INSERT – inserting a single row into a table

To insert a single row into a table, you use the following form of the INSERT statement:

1 INSERT INTO table1 (
2 column1,
3 column2 ,..)
4 VALUES
5 (
6 value1,
7 value2 ,...);
4
  • I still seem to be getting some errors when I run: ` db.execute("""INSERT INTO trading ( buyer, ticker, price, date) VALUES ( :buyer :ticker :price :date)""", buyer=session["user_id"], ticker=buy["symbol"], price=buy["price"], date=now)` I'll update them in the OP – Coal143 May 9 '18 at 13:05
  • You seem to be missing the commas between the various values. – Blauelf May 9 '18 at 13:27
  • 1
    AAAAAAAAAAAAAAAAARGH – Coal143 May 9 '18 at 13:37
  • For SQL, that's the only reasonable reaction. – Blauelf May 9 '18 at 13:42

You must log in to answer this question.

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