I have some trouble with the insertion into the database table in the buy section. I get a KeyError: "id". This is my code:

result = db.execute("INSERT INTO transactions (id, symbol, price, shares, date) VALUES(:username, :symbol, :price, :shares, :date)",
                id = session['user_id'],
                symbol = request.form.get("symbol"),
                price = price,
                shares = request.form.get("shares"),
                date = datetime.datetime.now())

This is the error https://ibb.co/g00dFV This is my table https://ibb.co/jitn9q


1 Answer 1


Use :id, not :username. The error appears because you have :username that is never set and id= that is never used.

Also, use style50, which outputs recommendations according to PEP8, which states in https://www.python.org/dev/peps/pep-0008/#other-recommendations

  • Don't use spaces around the = sign when used to indicate a keyword argument, or when used to indicate a default value for an unannotated function parameter.


  • Thanks a lot for pointing this out. I had forgotten to change :username to :id, :username was in a table I was previously trying to use and I had apparently left it like that. Now it works perfectly.
    – viet
    Nov 22, 2018 at 13:17

You must log in to answer this question.

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