谁能解释一下如何使用python将datetime插入到SQLite数据库中?哪种数据类型应该是日期时间,我尝试了许多不同的方法
我的疑问是基于cs50中的问题集7,我们使用flask作为web框架
history
是数据库的名称transaction
是字段的名称
db.execute("INSERT INTO history (transaction) VALUES(:d)",
d=datetime.datetime.today())
错误消息,我在运行应用程序时收到此消息
builtins.RuntimeError
RuntimeError: near "transaction": syntax error [SQL: "INSERT INTO history (transaction) VALUES('2019-05-19 17:14:25')"] (Background on this error at: http://sqlalche.me/e/e3q8)
发布于 2019-05-20 01:36:31
transaction
是SQLite中的reserved word。
sqlite> INSERT INTO history (transaction) VALUES('2019-05-19 17:14:25');
Error: near "transaction": syntax error
为了消除保留词的歧义,必须对它们进行特殊的引号。在这种情况下,作为带有双引号的标识符。
sqlite> INSERT INTO history ("transaction") VALUES('2019-05-19 17:14:25');
我建议重命名该列,而不是经常记住引用transaction
;对于行时间戳,created_at
是非常常见的。我也推荐using an ORM rather than writing SQL by hand;它将为您处理所有报价,并具有许多其他好处。
https://stackoverflow.com/questions/56210241
复制相似问题