I have a Runtime Error on a line of code in my buy route; could someone please tell me what I'm doing wrong? The error is "RuntimeError: 'str' object has no attribute 'key'". Line 68 in the code.

Here's my buy route:

@app.route("/buy", methods=["GET", "POST"])
def buy():
"""Buy shares of stock."""

if request.method == "POST":
    # ensure a symbol was submitted
    if not request.form.get("symbol"):
        return apology("symbol required")

    stock = lookup(request.form.get("symbol"))
    if not stock:
        return apology("symbol does not exist")

    user_input_shares = int(request.form.get("shares"))
    if user_input_shares < 0:
        return apology("number of shares cannot be negative!")

    user_cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
    if not user_cash:
        return apology("something went wrong when looking up cash amount")

    stock = lookup(request.form.get("symbol"))
    total = stock["price"] * user_input_shares
    if total > user_cash[0]["cash"]:
        return apology("You don't have enough money!")

    total_cash = user_cash[0]["cash"] - total
    db.execute("UPDATE users SET cash=:cash WHERE id=:id", cash=total_cash, id=session["user_id"])
    db.execute("INSERT INTO shares WHERE stock_name=:stock_name AND stock_price=:stock_price AND user_id=:user_id",
    stock["name"], stock["price"], session["user_id"])
    return render_template("index.html")
elif request.method == "GET":
    return render_template("buy.html")

If anyone wants to see my buy.html template, please let me know and I'll put it post a link to my Pastebin where I have it.

  • You have provided a lot of code, but not drawn our attention to exactly what line is 'Line 68', which is the cause of the RunTime Error. If you look in your IDE, you can identify what is 'Line 68'.
    – Rachel
    Mar 12, 2017 at 0:28
  • Please post what line is listed as Line 68 in your IDE! It helps us figure out how to help you! Mar 12, 2017 at 0:36

1 Answer 1


This is the only thing I can see:

db.execute("INSERT INTO shares WHERE stock_name=:stock_name AND stock_price=:stock_price AND user_id=:user_id", stock["name"], stock["price"], session["user_id"])

On this line, the syntax is wrong and you have not defined what stock_name, stock_price and user_id are properly.

An INSERT statement looks like this:

INSERT INTO table_name (field1, field2, field3) VALUES (value1, value2, value3)

SQLite Documentation | INSERT

On a side note, you don't need to call lookup() more than once.

  • Yeah, that's line 68. Sorry for not being clear enough. Why is it saying that a str object has no key member? stock and session are supposed to be dict objects, right? stock_price, stock_name and user_id are actual fields in my tables. Mar 12, 2017 at 14:25
  • It's a syntax problem. Rather than just stating the variables for db.execute to insert, you need to enter stock_name = stock["name"] etc. Mar 12, 2017 at 14:27
  • I changed it to db.execute("INSERT INTO shares(stock_name, stock_price, user_id) VALUES(:stock_name, :stock_price, :user_id)", stock_name=stock["name"], stock_price=stock["price"], user_id=session["user_id"]), but for some reason there are still no rows in my shares table. Why? And of course, since I haven't implemented index yet, I get the error that the template index.html can't be found. I'll fix that in a bit. Mar 12, 2017 at 14:46
  • When troubleshooting SQL, I like to enter the request directly into sqlite3 or phpliteadmin. If this works ok, maybe it's a problem with the variables you are passing in. Try printing them to the terminal to ensure they are what you think they are! Mar 12, 2017 at 17:51
  • How do I specify what database the table I'm trying to insert into is in? I tried running the query in SQLite3 on the terminal, but it gave me an error saying that there's no table called "shares". Mar 12, 2017 at 18:09

You must log in to answer this question.

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