0

I have been working on pset7, and I have been getting this error when trying to buy via the html form:

RuntimeError: (sqlite3.OperationalError) near "transaction": syntax error [SQL: "INSERT INTO history (date, id, transaction, symbol, shares, price) VALUES(datetime('now'), 1, 1, 'NKE', 4, 55.11)"]

My code is as follows:

# If the user can afford to buy the stocks
    if user_cash >= deal_cost:

        user_cash -= deal_cost
        #db.execute("create table if not exists portfolios(id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, sy TEXT, shares INTEGER, price REAL, date TEXT)");

        # Update the portfolios table
        db.execute("INSERT INTO portfolios (id, symbol, shares) VALUES (:user_id, :symbol, :shares)", user_id=user_id, symbol=symbol, shares=int(shares))

        # Update users cash after buying
        db.execute("UPDATE users SET cash = :user_cash WHERE id = :user_id", user_cash=user_cash, user_id=user_id)

        # Update the history table
        db.execute("INSERT INTO history (date, id, transaction, symbol, shares, price) VALUES(datetime('now'), :user_id, :transaction, :symbol, :shares, :price)", user_id=user_id, transaction=1, symbol=symbol, shares=int(shares), price= info["price"])

Can you help me figure out the problem?

1

transaction is a sqlite reserved word. You'll need to escape the column name with quotes (eg 'transaction') or create the table again and rename the column.

| improve this answer | |
  • Thanks! that worked. – atefsawaed Apr 10 '17 at 12:12

You must log in to answer this question.

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