2016-01-15 24 views
-1

我正在使用SQLite3數據庫文件來檢索數據並將其存儲在變量中,以便我可以使用它在HTML表格上顯示結果。在python中顯示Sqlite3結果到HTML表中

這是我如何存儲從查詢結果:

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

然而,當我嘗試在表中顯示它,使用"{%for x in test%}",正在顯示這樣的輸出:(1.234,)。我需要爲輸出更改如下所示:1.234所以沒有括號和逗號?

+0

'test = test [0]' – gtlambert

回答

1

您希望您的查詢的所有結果的列表,你應該使用fetchall(),其中,根據文檔:

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> 
+0

「TypeError:'sqlite3.Cursor'對象不是su bscriptable「我在 – Ghostali

+0

之前嘗試過使用適當的方法更新了答案。 –

+0

好吧我試過這個,並得到這個錯誤「AttributeError:'sqlite3.Connection'對象沒有屬性'fetchone'」我也在做這個「test = c.execute(」SELECT column_name from table_name LIMIT 11)「 – Ghostali