pythonsqliteflask

not able to extract specific data in sqlite table using flask python application


I am developing a flask app where I have a table named "TDS" inside sqlite database.

enter image description here

I want to access row wise specific data for example in column "Income_under_salary" in first row.

Code is .....

tds = TDS.query.all()
for td in tds:
    result = td.Income_under_salary
    print(result[0])

Here I am expecting to get answer "9", but I get "9 415244 38024" all three.

Type of "result" shows <class 'int'> <class 'int'> <class 'int'>

Since "result" is "int object" it is not iterable.

The HTML code:

{% for item in result %}
    <div> {{item}} </div>
{% endfor %}

The HTML also gives "TypeError: 'int' object is not iterable"

I am not sure even after mentioning index, why I am getting three answers?

Can someone help me to correct this code?


Solution

  • Make result a list of the column value from each row:

    result = [td.Income_under_salary for td in tds]
    

    Then the Flask template will put each value in its own <div>.