在python的HTML表中显示Sqlite3结果 [英] Displaying Sqlite3 results in a HTML table with python
问题描述
我正在使用SQLite3 db文件检索数据并将其存储在变量中,以便可以使用它在HTML表格上显示结果。
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")
但是,当我尝试使用在表中显示它时,{ %for测试中的x%} ,输出显示如下:
(1.234,)
。我需要更改什么才能使输出看起来像这样: 1.234
,因此没有括号和逗号?
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?
推荐答案
要获取所有查询结果的列表,应使用 fetchall()
,根据docs:
You want a list of all results of your query, you should use fetchall()
, which, according to the docs:
获取查询结果的所有(剩余)行,并返回一个列表。请注意,
的游标的arraysize属性会影响此操作
的性能。当没有行可用时,将返回一个空列表。
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.
尝试以下操作:
c = sqlite3.connect('db_path.db')
cur = c.cursor()
cur.execute("SELECT column_name from table_name LIMIT 11")
test = cur.fetchall()
您的html如下所示:
Your html would then look like this:
<table>
{% for row in test %}
<tr>
<th> {{ row[0] }}</th>
</tr>
{% endfor %}
</table>
这篇关于在python的HTML表中显示Sqlite3结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!