加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 19:14:04 所属栏目:百科 来源:网络整理
导读:定义关系时,我想在insert插入时更新属性到时间戳。例如,我现在有一个工作表 CREATE TABLE t1(id INTEGER PRIMARY KEY AUTOINCREMENT,time TIMESTAMPDEFAULT CURRENT_TIMESTAMP,txt TEXT); 这是更新插入的时间戳,例如insert into t1(txt)values(‘hello’)
定义关系时,我想在insert插入时更新属性到时间戳。例如,我现在有一个工作表
CREATE TABLE t1(
id INTEGER PRIMARY KEY AUTOINCREMENT,time TIMESTAMP
DEFAULT 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|foo
2|1342686321|bar
3|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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读