如何在SQLite中拥有自动时间戳?

时间:2022-12-07 13:35:17

I have an SQLite database, version 3 and I am using C# to create an application that uses this database.

我有一个SQLite数据库,版本3,我正在使用C#创建一个使用该数据库的应用程序。

I want to use a timestamp field in a table for concurrency, but I notice that when I insert a new record, this field is not set, and is null.

我想在表中使用timestamp字段进行并发,但我注意到当我插入新记录时,此字段未设置,并且为null。

For example, in MS SQL Server if I use a timestamp field this is updated by the database, I have not to set by myself. is this possible in SQLite?

例如,在MS SQL Server中,如果我使用时间戳字段,这由数据库更新,我不必自己设置。这在SQLite中可能吗?

6 个解决方案

#1


160  

Just declare a default value for a field:

只需声明字段的默认值:

CREATE TABLE MyTable(
    ID INTEGER PRIMARY KEY,
    Name TEXT,
    Other STUFF,
    Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);

However, if your INSERT command explicitly sets this field to NULL, it will be set to NULL.

但是,如果您的INSERT命令显式将此字段设置为NULL,则它将设置为NULL。

#2


59  

You can create TIMESTAMP field in table on the SQLite, see this:

您可以在SQLite上的表中创建TIMESTAMP字段,请参阅:

CREATE TABLE my_table (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    name VARCHAR(64),
    sqltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);

INSERT INTO my_table(name, sqltime) VALUES('test1', '2010-05-28T15:36:56.200');
INSERT INTO my_table(name, sqltime) VALUES('test2', '2010-08-28T13:40:02.200');
INSERT INTO my_table(name) VALUES('test3');

This is the result:

这是结果:

SELECT * FROM my_table;

如何在SQLite中拥有自动时间戳?

#3


7  

Reading datefunc a working example of automatic datetime completion would be:

读取datefunc自动日期时间完成的一个工作示例是:

sqlite> CREATE TABLE 'test' ( 
   ...>    'id' INTEGER PRIMARY KEY,
   ...>    'dt1' DATETIME NOT NULL DEFAULT (datetime(CURRENT_TIMESTAMP, 'localtime')), 
   ...>    'dt2' DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')), 
   ...>    'dt3' DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime'))
   ...> );

Let's insert some rows in a way that initiates automatic datetime completion:

让我们以启动自动日期时间完成的方式插入一些行:

sqlite> INSERT INTO 'test' ('id') VALUES (null);
sqlite> INSERT INTO 'test' ('id') VALUES (null);

The stored data clearly shows that the first two are the same but not the third function:

存储的数据清楚地显示前两个是相同但不是第三个功能:

sqlite> SELECT * FROM 'test';
1|2017-09-26 09:10:08|2017-09-26 09:10:08|2017-09-26 09:10:08.053
2|2017-09-26 09:10:56|2017-09-26 09:10:56|2017-09-26 09:10:56.894

Pay attention that SQLite functions are surrounded in parenthesis! How difficult was this to show it in one example?

注意SQLite函数包含在括号中!在一个例子中展示它有多困难?

Have fun!

玩的开心!

#4


2  

you can use the custom datetime by using...

你可以使用自定义日期时间...

 create table noteTable3 
 (created_at DATETIME DEFAULT (STRFTIME('%d-%m-%Y   %H:%M', 'NOW','localtime')),
 title text not null, myNotes text not null);

use 'NOW','localtime' to get the current system date else it will show some past or other time in your Database after insertion time in your db.

使用'NOW','localtime'来获取当前系统日期,否则它会在数据库中插入时间后显示数据库中的过去或其他时间。

Thanks You...

谢谢...

#5


2  

To complement answers above...

为了补充上面的答案......

If you are using EF, adorn the property with Data Annotation [Timestamp], then go to the overrided OnModelCreating, inside your context class, and add this Fluent API code:

如果您使用EF,请使用Data Annotation [Timestamp]装饰该属性,然后转到上下文类中的重写OnModelCreating,并添加此Fluent API代码:

modelBuilder.Entity<YourEntity>()
                .Property(b => b.Timestamp)
                .ValueGeneratedOnAddOrUpdate()
                .IsConcurrencyToken()
                .ForSqliteHasDefaultValueSql("CURRENT_TIMESTAMP");

It will make a default value to every data that will be insert into this table.

它将为将要插入此表的每个数据设置默认值。

#6


0  

you can use triggers. works very well

你可以使用触发器。效果很好

CREATE TABLE MyTable(
ID INTEGER PRIMARY KEY,
Name TEXT,
Other STUFF,
Timestamp DATETIME);


CREATE TRIGGER insert_Timestamp_Trigger
AFTER INSERT ON MyTable
BEGIN
   UPDATE MyTable SET Timestamp =STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') WHERE id = NEW.id;
END;

CREATE TRIGGER update_Timestamp_Trigger
AFTER UPDATE On MyTable
BEGIN
   UPDATE MyTable SET Timestamp = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') WHERE id = NEW.id;
END;

#1


160  

Just declare a default value for a field:

只需声明字段的默认值:

CREATE TABLE MyTable(
    ID INTEGER PRIMARY KEY,
    Name TEXT,
    Other STUFF,
    Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);

However, if your INSERT command explicitly sets this field to NULL, it will be set to NULL.

但是,如果您的INSERT命令显式将此字段设置为NULL,则它将设置为NULL。

#2


59  

You can create TIMESTAMP field in table on the SQLite, see this:

您可以在SQLite上的表中创建TIMESTAMP字段,请参阅:

CREATE TABLE my_table (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    name VARCHAR(64),
    sqltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);

INSERT INTO my_table(name, sqltime) VALUES('test1', '2010-05-28T15:36:56.200');
INSERT INTO my_table(name, sqltime) VALUES('test2', '2010-08-28T13:40:02.200');
INSERT INTO my_table(name) VALUES('test3');

This is the result:

这是结果:

SELECT * FROM my_table;

如何在SQLite中拥有自动时间戳?

#3


7  

Reading datefunc a working example of automatic datetime completion would be:

读取datefunc自动日期时间完成的一个工作示例是:

sqlite> CREATE TABLE 'test' ( 
   ...>    'id' INTEGER PRIMARY KEY,
   ...>    'dt1' DATETIME NOT NULL DEFAULT (datetime(CURRENT_TIMESTAMP, 'localtime')), 
   ...>    'dt2' DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')), 
   ...>    'dt3' DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime'))
   ...> );

Let's insert some rows in a way that initiates automatic datetime completion:

让我们以启动自动日期时间完成的方式插入一些行:

sqlite> INSERT INTO 'test' ('id') VALUES (null);
sqlite> INSERT INTO 'test' ('id') VALUES (null);

The stored data clearly shows that the first two are the same but not the third function:

存储的数据清楚地显示前两个是相同但不是第三个功能:

sqlite> SELECT * FROM 'test';
1|2017-09-26 09:10:08|2017-09-26 09:10:08|2017-09-26 09:10:08.053
2|2017-09-26 09:10:56|2017-09-26 09:10:56|2017-09-26 09:10:56.894

Pay attention that SQLite functions are surrounded in parenthesis! How difficult was this to show it in one example?

注意SQLite函数包含在括号中!在一个例子中展示它有多困难?

Have fun!

玩的开心!

#4


2  

you can use the custom datetime by using...

你可以使用自定义日期时间...

 create table noteTable3 
 (created_at DATETIME DEFAULT (STRFTIME('%d-%m-%Y   %H:%M', 'NOW','localtime')),
 title text not null, myNotes text not null);

use 'NOW','localtime' to get the current system date else it will show some past or other time in your Database after insertion time in your db.

使用'NOW','localtime'来获取当前系统日期,否则它会在数据库中插入时间后显示数据库中的过去或其他时间。

Thanks You...

谢谢...

#5


2  

To complement answers above...

为了补充上面的答案......

If you are using EF, adorn the property with Data Annotation [Timestamp], then go to the overrided OnModelCreating, inside your context class, and add this Fluent API code:

如果您使用EF,请使用Data Annotation [Timestamp]装饰该属性,然后转到上下文类中的重写OnModelCreating,并添加此Fluent API代码:

modelBuilder.Entity<YourEntity>()
                .Property(b => b.Timestamp)
                .ValueGeneratedOnAddOrUpdate()
                .IsConcurrencyToken()
                .ForSqliteHasDefaultValueSql("CURRENT_TIMESTAMP");

It will make a default value to every data that will be insert into this table.

它将为将要插入此表的每个数据设置默认值。

#6


0  

you can use triggers. works very well

你可以使用触发器。效果很好

CREATE TABLE MyTable(
ID INTEGER PRIMARY KEY,
Name TEXT,
Other STUFF,
Timestamp DATETIME);


CREATE TRIGGER insert_Timestamp_Trigger
AFTER INSERT ON MyTable
BEGIN
   UPDATE MyTable SET Timestamp =STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') WHERE id = NEW.id;
END;

CREATE TRIGGER update_Timestamp_Trigger
AFTER UPDATE On MyTable
BEGIN
   UPDATE MyTable SET Timestamp = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') WHERE id = NEW.id;
END;