3
我使用Flask在頁面上顯示日期和溫度。數據存儲在SQLite數據庫中。它與字符串的日期和時間很好,但我試圖將日期和時間轉換爲unix時代時遇到了問題。從字符串到字符串unixepoch的日期轉換
Python代碼:
cur = db.execute('select date,temperature from temperatures where temperatures.date > datetime(\'now\',\'-1hour\', \'localtime\') order by date desc;')
entries = cur.fetchall()
return render_template('dashboard2.html', entries=entries)
模板:
{% for entry in entries %}
{{ entry.date }} --- {{ entry.temperature }}
{% endfor %}
結果是:
2013-04-08 21:26:22 --- 22.31
我想要做的事:
sqlite> select strftime('%s',date),temperature from temperatures where temperatures.date > datetime('now','-1hour', 'localtime') order by date desc;
給出(SQLite中控制檯):
(...)
1365340231|22.56
1365340221|22.56
1365340210|22.5
1365340199|22.56
(...)
但:
cur = db.execute('select strftime(\'%s\',date),temperature from temperatures where temperatures.date > datetime(\'now\',\'-1hour\', \'localtime\') order by date desc;')
這個選擇給出燒瓶中entry.temperature在entry.date空值和預期值模板:
--- 22.31
我在下面發佈瞭解決方案。
無關:請記住,您可以互換使用兩種引用。如果你使用'「」'來編寫SQL查詢,你不必轉義引號。 – liori 2013-04-07 16:55:58
哪個版本的SQLite是這樣的?使用'-1hour'修飾符(在數字和'小時之間沒有空格)我使用'datetime()'函數獲得'NULL',而使用'-1 hour'返回一個日期。我正在使用SQLite 3.7.16。 – 2013-04-07 18:38:04
@AudriusKažukauskasSQLite 3.6.22。朋友已經幫我解決問題了。看起來我在解釋問題時犯了一些錯誤。我會盡力修復它。 – 2013-04-08 19:28:43