【发布时间】:2020-11-06 12:20:06
【问题描述】:
大家,我正在编写与交易相关的程序,在一个视图中,我正在呈现交易的历史记录,由于某种原因,created_at 列没有呈现交易发生的日期。有小费吗? 这是application.py:
def history():
transactions = db.execute("SELECT symbol, shares, shares_price, created_at FROM bonds WHERE user_id = :user_id ORDER BY created_at ASC", user_id = session["user_id"])
return render_template("history.html", transactions=transactions)
这里是视图:
{% for t in transactions %}
<tr>
<td>{{ t.symbol }}</td>
<td>{{ t.shares }}</td>
<td>{{ (t.shares * t.shares_price) | usd }}</td>
<td>{{ t.created_at }}</td>
</tr>
{% endfor %}
最后是数据库文件,以备大家需要:
column: created_at
datatype: DATETIME
NULL: No
default value: None
primary key: No
【问题讨论】:
-
sqlite 没有内部DATETIME data type。如何插入行?您是否查看过其他工具中的数据,例如命令行 sqlite3?
-
我打开 sqlite3 终端并运行 ALTER TABLE Bonds ADD COLUMN created_at DATETIME 并将其插入数据库。我真的不明白你的意思是它没有日期时间。你能详细说明一下吗? @DinoCoderSaurus
-
@DinoCoderSaurus 你将如何保存日期并显示?
-
链接中的文档对它的描述比我以往任何时候都好。将行插入到债券表中时,数据将“保存”在数据库中。
-
我不相信这就是问题所在。 Python dbs 确实支持 datetime 类型,如果不支持,我将不允许将该列添加到表中。问题在于更新并在视图@DinoCoderSaurus 上显示它
标签: python database sqlite flask