如何在SQLite中具有自动时间戳? [英] How to have an automatic timestamp in SQLite?
问题描述
我有一个SQLite数据库,版本3,我正在使用C#创建使用该数据库的应用程序。
I have an SQLite database, version 3 and I am using C# to create an application that uses this database.
我想在表中使用时间戳字段并发性,但是我注意到当我插入新记录时,未设置此字段,并且该字段为null。
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.
例如,在MS SQL Server中,如果我使用时间戳字段由数据库更新,我不必自己设置。
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?
推荐答案
只需为字段声明默认值:
Just declare a default value for a field:
CREATE TABLE MyTable(
ID INTEGER PRIMARY KEY,
Name TEXT,
Other STUFF,
Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
但是,如果您的 INSERT
命令明确设置此字段为 NULL
,它将设置为 NULL
。
However, if your INSERT
command explicitly sets this field to NULL
, it will be set to NULL
.
这篇关于如何在SQLite中具有自动时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!