SQLite将默认时间戳记存储为unixepoch

SQLite将默认时间戳记存储为unixepoch,第1张

概述定义关系时,我想在insert插入时更新属性到时间戳。例如,我现在有一个工作表 CREATE TABLE t1(id INTEGER PRIMARY KEY AUTOINCREMENT,time TIMESTAMPDEFAULT CURRENT_TIMESTAMP,txt TEXT); 这是更新插入的时间戳,例如insert into t1(txt)values(‘hello’)添加行1 定义关系时,我想在insert插入时更新属性到时间戳。例如,我现在有一个工作表
CREATE table t1(ID INTEGER PRIMARY KEY autoINCREMENT,time TIMESTAMPDEFAulT CURRENT_TIMESTAMP,txt TEXT);

这是更新插入的时间戳,例如insert into t1(txt)values(‘hello’)添加行1 | 2012-07-19 08:07:20 | hello |。但是,我希望将此日期格式化为unixepoch格式。

我读了docs,但这还不清楚。例如,我将表关系修改为TIMESTAMP DEFAulT DATETIME(‘Now’,’unixepoch’),但是我收到错误。在这里,如在文档中,现在是我的时间字符串和unixepoch是修饰符,但它没有工作。有人可以帮助我如何将其格式化为unixepoch时间戳?

使用strftime:
sqlite> select strftime('%s','Now');1342685993

在CREATE table中使用它,如下所示:

sqlite> create table t1 (   ...> ID integer primary key,...> time timestamp default (strftime('%s','Now')),...> txt text);sqlite> insert into t1 (txt) values ('foo');sqlite> insert into t1 (txt) values ('bar');sqlite> insert into t1 (txt) values ('baz');sqlite> select * from t1;1|1342686319|foo2|1342686321|bar3|1342686323|baz

见https://www.sqlite.org/lang_createtable.html#tablecoldef

If the default value of a column is an Expression in parentheses,then the Expression is evaluated once for each row inserted and the results used in the new row.

总结

以上是内存溢出为你收集整理的SQLite将默认时间戳记存储为unixepoch全部内容,希望文章能够帮你解决SQLite将默认时间戳记存储为unixepoch所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1161561.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存