SQLite almacenando la marca de tiempo predeterminada como unixepoch


Al definir una relación, quiero actualizar un atributo a la marca de tiempo en insert. Por ejemplo, una mesa de trabajo que tengo ahora

CREATE TABLE t1(
id INTEGER PRIMARY KEY AUTOINCREMENT,
time TIMESTAMP
DEFAULT CURRENT_TIMESTAMP,
txt TEXT);

Esto es actualizar una marca de tiempo en insert, por ejemplo, insert into t1 (txt) values ('hello') agrega la fila 1|2012-07-19 08:07:20|hello|. Sin embargo, quiero tener esta fecha formateada en formato unixepoch.

Leí losdocumentos pero esto no estaba claro. Por ejemplo, modifiqué la relación de la tabla a time TIMESTAMP DEFAULT DATETIME('now','unixepoch') pero obtengo un error. Aquí, como en los documentos, now era mi cadena de tiempo y unixepoch fue el modificador pero no funcionó. ¿Alguien podría ayudarme a formatearlo como una marca de tiempo unixepoch?

Author: yayu, 2012-07-19

3 answers

Use strftime:

sqlite> select strftime('%s', 'now');
1342685993

Úsalo en CREATE TABLE así:

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

Véase https://www.sqlite.org/lang_createtable.html#tablecoldef

Si el valor predeterminado de una columna es una expresión entre paréntesis, entonces la expresión se evalúa una vez para cada fila insertada y los resultados utilizados en la nueva fila.

 42
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-19 08:26:29

Tenga en cuenta que 'timestamp' no es un tipo de datos conocido por SQLite (consulte la lista aquí). El valor predeterminado generado por strftime () se almacenaría como Texto.

Si es importante almacenar el valor como un número en lugar de como una cadena, declare el campo como un entero y agregue un CAST() a la mezcla, así:

create table t1(
...
ts_field integer(4) default (cast(strftime('%s','now') as int)),
...
);
 12
Author: user3704578,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-02 18:33:06

De hecho strftime, que también se puede usar de la siguiente manera:

SELECT strftime('%s', timestamp) as timestamp FROM ... ;

Te da:

1454521888

La columna de la tabla'timestamp' puede ser un campo de texto incluso, usando el current_timestamp por DEFECTO.

Sin strftime:

SELECT timestamp FROM ... ;

Te da:

2016-02-03 17:51:28

 2
Author: danger89,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-03 18:02:02