如何在 SQLite 中设置自动时间戳? [英] How to have an automatic timestamp in SQLite?

查看:36
本文介绍了如何在 SQLite 中设置自动时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想在一个表中使用时间戳字段进行并发,但我注意到当我插入一条新记录时,该字段没有设置,并且为空.

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 中,如果我使用由数据库更新的时间戳字段,则不必自己设置.这在 SQLite 中可行吗?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆