【发布时间】:2010-10-19 18:34:56
【问题描述】:
说,我们创建了一个表:
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')”文本。
【问题讨论】:
-
这里的一个问题是Android 使用SQLite 作为数据库。您可以将列设置为某种类型,但这只是将列设置为“亲和力”。 SQLite 将允许您在任何列中放置任何值,而不管它是如何声明的。对于 SQLite,将字符串 'date' 放在任何地方都可以。 sqlite.org 有更详尽的解释。我在下面投票支持 e-satis 的答案,我就是这样做的(特别是 Java 方式)。
标签: android sqlite content-values