使用python在HTML表中显示Sqlite3结果

2024-04-25 13:24:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用一个SQLite3 db文件来检索数据并将其存储在一个变量中,以便可以使用它在HTML表上显示结果。

这就是我存储查询结果的方式:

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

但是,当我尝试使用"{%for x in test%}"将其显示在表中时,输出的显示方式如下:(1.234,)。我需要更改什么才能使输出看起来像这样:1.234所以没有括号和逗号?


Tags: 文件数据namefromtestforexecutedb
1条回答
网友
1楼 · 发布于 2024-04-25 13:24:17

如果想要查询的所有结果的列表,则应使用^{},根据文档:

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将如下所示:

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

相关问题 更多 >

    热门问题