Ghostali
Ghostali

Reputation: 73

Displaying Sqlite3 results in a HTML table with python

I am using a SQLite3 db file to retrieve data and store it in a variable so that I can use it to display the results on a HTML table.

This is how I am storing the results from the query:

test = c.execute("SELECT column_name from table_name LIMIT 11")

However, when I try to display it in the table, using "{%for x in test%}", the output is being displayed like this: (1.234,). What do I need to change for the output to look like this: 1.234 so without the bracket and comma?

Upvotes: 0

Views: 12998

Answers (1)

Gabriel Ilharco
Gabriel Ilharco

Reputation: 1679

You want a list of all results of your query, you should use fetchall(), which, according to the docs:

Fetches all (remaining) rows of a query result, returning a list. Note that the cursor’s arraysize attribute can affect the performance of this operation. An empty list is returned when no rows are available.

Try this:

c = sqlite3.connect('db_path.db')
cur = c.cursor()
cur.execute("SELECT column_name from table_name LIMIT 11")
test = cur.fetchall()

Your html would then look like this:

<table>
  {% for row in test %}
  <tr>
    <th> {{ row[0] }}</th>
  </tr> 
  {% endfor %}
</table>

Upvotes: 2

Related Questions