在SQLite中插入时间戳保持为空

时间:2012-02-15 10:31:30

标签: android sqlite timestamp

我正在使用以下代码将带有时间戳列的行插入到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。其他列(代码示例中未显示)是可以的。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

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

并按字符串检索如下:

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

P.S。确保您的列的类型为DATETIME