【问题标题】:Insert timestamp in SQLite stays null在 SQLite 中插入时间戳保持为空
【发布时间】:2012-02-15 10:31:29
【问题描述】:

我正在使用以下代码将带有时间戳列的行插入 SQLite 数据库:

ContentValues contentValues = new ContentValues();
contentValues.put(LAST_UPDATE, "15-02-2012 11:27:42");
database.insert(TABLE_NAME, null, contentValues );

当我之后检查表格时,带有时间戳的列(定义为timestamp not null)始终是null。其他列(代码示例中未显示)都可以。有什么想法吗?

【问题讨论】:

  • 我认为日期时间的格式应该是YYYY-MM-DD HH:MM:SS,您使用的是DD-MM-YYYY HH:MM:SS。尝试更改它,看看它是否有效。另外..您是设置contentValues,但随后将initialValues 用于INSERT
  • 更改日期格式不会更改结果。 initialValues 是一个错字。

标签: android sqlite timestamp


【解决方案1】:

您应该使用以下日期格式以便在 SQLite 中插入日期/时间:

DateFormat dateFormatISO8601 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

并在字符串中检索如下:

String crntDate = dateFormatISO8601.format(new Date());

附:确保您的列的类型为 DATETIME

【讨论】:

  • 我将列类型切换为datetime,现在可以了。我真的以为我以前成功使用过timestamp
  • 我猜时间戳有不同的格式,但无论如何,很高兴知道它对你有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
相关资源
最近更新 更多