开发者社区 问答 正文

如何在Android应用程序中插入日期时间设置为“现在”的SQLite记录?

说,我们有一个表创建为:

create table notes (_id integer primary key autoincrement, created_date date) 要插入记录,我会使用

ContentValues initialValues = new ContentValues(); initialValues.put("date_created", ""); long rowId = mDb.insert(DATABASE_TABLE, null, initialValues); 但是如何将date_created列设置为now?为了明确起见,

initialValues.put("date_created", "datetime('now')"); 不是正确的解决方案。它只是将列设置为“ datetime('now')”文本。

展开
收起
Puppet 2020-01-19 13:49:43 561 分享 版权
1 条回答
写回答
取消 提交回答
  • 在我的代码中,我DATETIME DEFAULT CURRENT_TIMESTAMP用作列的类型和约束。

    在您的情况下,您的表定义为

    
    create table notes (
      _id integer primary key autoincrement, 
      created_date date default CURRENT_DATE
    )
    
    2020-01-19 13:50:06
    赞同 展开评论