SQLite将默认时间戳存储为unixepoch
定义关系时,我希望在插入时将属性更新为时间戳。例如,我现在有一个工作台SQLite将默认时间戳存储为unixepoch,sqlite,timestamp,Sqlite,Timestamp,定义关系时,我希望在插入时将属性更新为时间戳。例如,我现在有一个工作台 CREATE TABLE t1( id INTEGER PRIMARY KEY AUTOINCREMENT, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, txt TEXT); 这是在插入时更新时间戳,例如,插入t1(txt)值('hello')添加行1 | 2012-07-19 08:07:20 | hello |。但是,我希望将此日期格式化为unixepoch格式 我看了报纸,但不
CREATE TABLE t1(
id INTEGER PRIMARY KEY AUTOINCREMENT,
time TIMESTAMP
DEFAULT CURRENT_TIMESTAMP,
txt TEXT);
这是在插入时更新时间戳,例如,插入t1(txt)值('hello')
添加行1 | 2012-07-19 08:07:20 | hello |
。但是,我希望将此日期格式化为unixepoch格式
我看了报纸,但不清楚。例如,我将表关系修改为
time-TIMESTAMP-DEFAULT-DATETIME('now','unixepoch')
,但我得到了一个错误。在这里,正如在文档中一样,now
是我的时间字符串,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
看
如果列的默认值是括号中的表达式,则对于插入的每一行以及新行中使用的结果,将对表达式求值一次
注“timestamp”不是SQLite已知的数据类型(请参阅列表)。strftime()生成的默认值实际上将存储为文本 如果将值存储为数字而不是字符串很重要,请将字段声明为整数并在混合中添加CAST(),如下所示:
create table t1(
...
ts_field integer(4) default (cast(strftime('%s','now') as int)),
...
);
实际上,strftime也可以这样使用:
SELECT strftime('%s', timestamp) as timestamp FROM ... ;
给你:
1454521888
“timestamp”表列甚至可以是文本字段,使用current\u timestamp
作为默认值
没有strftime:
SELECT timestamp FROM ... ;
给你:
2016-02-03 17:51:28
整型主键不需要自动递增
。