I'm trying to get all stocks to just show up on my index screen, but can't seem to get my head around the jinja template + Python + SQL commands.

Can someone tell me why is it that only my last bought share shows up and my other stocks don't show up? Is it because of my flawed "for" loop execution?

Here's my Python code:

def index():

    shares = db.execute("SELECT symbol, price, name, SUM(shares) AS quanties FROM shares WHERE id=:id", id=session["user_id"])

    for share in shares:
        symbol= str(share["symbol"])
        price= (share["price"])
        sname= str(share["name"])
        quantity= (share["quanties"])     #share["shares"] need to corelate/be reflected in HTML?

    rows=db.execute("SELECT * FROM users where id = :id", id = session["user_id"])
    return render_template("index.html", shares=shares, sname=sname, quantity=quantity, price=price, name=rows[0]["username"])


{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}


        <li><h2> {{ name }}</h2></li>

<table class="table table-striped">

            <th>Share Symbol</th>
            <th>Shares Name</th>
            <th>Number of Shares</th>
            <th>Current price</th>
            <th>Total value</th>

         {% for share in shares %}
                <td>{{ share.symbol }}</td>
                <td>{{ share.name }}</td>
                <td>{{ share.quanties }}</td>     <!--this nees to corelate to quantity=(share["shares"]), not share["quantity"])-->
                <td>{{ share.price }}</td>
        {% endfor %}

{% endblock %}

result: It shows all my shares "SUM"ed in the SQL command. But does not display the rest of the symbol and names of other stocks i own.. :(

enter image description here

1 Answer 1


This sql "SELECT symbol, price, name, SUM(shares) AS quanties FROM shares WHERE id=:id" always returns one row. Try it in sqlite3/phpliteadmin. You need a GROUP BY clause . Since you (presumably) want one row per symbol, you would add GROUP BY symbol after the where clause (try that in sqlite3/phpliteadmin to see the difference).

  • Thanks for that!
    – nvs0000
    Commented May 18, 2017 at 19:16

You must log in to answer this question.

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