显示Sqlite3导致使用python的HTML表

时间:2016-01-15 14:30:21

标签: python database flask sqlite

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

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

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

但是,当我尝试使用"{%for x in test%}"在表格中显示它时,输出显示如下:(1.234,)。我需要更改输出,如下所示:1.234所以没有括号和逗号?

1 个答案:

答案 0 :(得分:2)

您想要查询所有查询结果的列表,您应该使用fetchall(),根据文档:

  

获取查询结果的所有(剩余)行,返回一个列表。注意   游标的arraysize属性可以影响性能   这个操作。没有行可用时返回空列表。

试试这个:

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>
相关问题