在sqlite中创建一个具有默认当前时间的日期时间列



我有一个sqlite数据对象,比如"Ticket"。我在其中有一些列字段,这些字段是根据从服务器收到的日期填充的。

我想添加一个日期时间列,每当创建记录时,该列必须将其值设置为默认的当前日期时间。

public class Ticket : BaseNotifyPropertyChanged
{
[PrimaryKey, AutoIncrement]
public int MobileID { get; set; }
public int TicketID { get; set; }
//new column to be added with default datetime
public DateTime test
}

试试这个:

public DateTime test { get; set; } = DateTime.Now;

只有在与上述完全相同的情况下,这才会起作用。如果您编写自定义getset,那么我认为您可以设置备份字段的默认值,例如:

DateTime _time = DateTime.Now;
public DateTime Time { 
get{
return _time;
} 
set {
if (_time != value) {
_time = value;
}
} 
}

您可以进行

CREATE TABLE mytable 
( 
MobileID BIGINT (14) NOT NULL, 
TicketID BIGINT(14) NOT NULL, 
date     DATETIME NOT NULL DEFAULT (CURRENT_TIMESTAMP), 
PRIMARY KEY (MobileID, TicketID ) 
); 

最新更新