I have created a table named "portfolio" in my database which stores users purchase data while I kept the user_id field of this table NOT UNIQUE ie no primary key so as to let the user buy more same or other stock. But if I query portfolio now for displaying it in index.html as

rows = db.execute("SELECT * FROM portfolio WHERE id = :id", id=session["user_id"]")

how do I access the field data of all of the users rows ie rows[0]["Symbol"] will give me just the Symbol field of primary row of the user whereas I need all of his rows? Any suggestions will be appreciated!

2 Answers 2


rows holds all the purchases that this user has done. You just have to iterate over the rows like so for example:

for row in rows:

I'm not sure that's what you want. If not, edit your question and clarify what you want.

  • My debbuger is showing an error on using rows["Symbol"] says : TypeError: list indices must be integers, not str . Apologies for not being clear but my only point was to say that str SELECT will return list of dict then will rows only return the current user's row or will also have other user's row as when rows = db.execute("SELECT * FROM users WHERE username = :Username", Username=requset.form.get("username") session["user_id"] = rows[0]["id"] which means rows gives the username row in the first row whereas other rows are not belonging to the user.
    – sk.76
    Commented Jul 21, 2017 at 18:02
  • I still don't quite get what you mean. Could you add a print(rows) statement after the select and post the output?
    – ChrisG
    Commented Jul 21, 2017 at 18:36
  • 1
    Apologies for not being clear. I was kinda like stuck on this for long so couldn't spot the mistake I was making. Anyway, the problem is now solved and thanks for help!
    – sk.76
    Commented Jul 22, 2017 at 9:09

To iterate over the rows and the data in each do something like:

    for i in range(len(rows)):
            symbol = rows[i]["symbol"]
            shares = rows[i]["shares"]
            name = rows[i]["name"]

You must log in to answer this question.

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