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

dro*_*guy 107 sqlite android content-values

比如说,我们创建了一个表格:

create table notes (_id integer primary key autoincrement, created_date date)
Run Code Online (Sandbox Code Playgroud)

要插入记录,我会使用

ContentValues initialValues = new ContentValues(); 
initialValues.put("date_created", "");
long rowId = mDb.insert(DATABASE_TABLE, null, initialValues);
Run Code Online (Sandbox Code Playgroud)

但是如何将date_created列设置为now?为了说清楚,

initialValues.put("date_created", "datetime('now')");
Run Code Online (Sandbox Code Playgroud)

不是正确的解决方案.它只是将列设置为"datetime('now')"文本.

e-s*_*tis 191

您不能使用Java包装器"ContentValues"来使用datetime函数.您可以使用:

  • SQLiteDatabase.execSQL因此您可以输入原始SQL查询.

    mDb.execSQL("INSERT INTO "+DATABASE_TABLE+" VALUES (null, datetime()) ");
    
    Run Code Online (Sandbox Code Playgroud)
  • 或者java日期时间功能:

    // set the format to sql date time
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date date = new Date();
    ContentValues initialValues = new ContentValues(); 
    initialValues.put("date_created", dateFormat.format(date));
    long rowId = mDb.insert(DATABASE_TABLE, null, initialValues);
    
    Run Code Online (Sandbox Code Playgroud)

  • java.util.Date,但我相信你也可以用java.sql.Date日期来做. (5认同)
  • 是java.util.Date还是java.sql.Date? (3认同)

Gau*_*oun 43

在我的代码中,我DATETIME DEFAULT CURRENT_TIMESTAMP用作列的类型和约束.

在您的情况下,您的表定义将是

create table notes (
  _id integer primary key autoincrement, 
  created_date date default CURRENT_DATE
)
Run Code Online (Sandbox Code Playgroud)

  • 你读取值时使用哪种java类型? (2认同)

Rik*_*tel 31

方法1

CURRENT_TIME – Inserts only time
CURRENT_DATE – Inserts only date
CURRENT_TIMESTAMP – Inserts both time and date

CREATE TABLE users(
    id INTEGER PRIMARY KEY,
    username TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Run Code Online (Sandbox Code Playgroud)

方法2

db.execSQL("INSERT INTO users(username, created_at) 
            VALUES('ravitamada', 'datetime()'");
Run Code Online (Sandbox Code Playgroud)

方法3: 使用java Date函数

private String getDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);
}

ContentValues values = new ContentValues();
values.put('username', 'ravitamada');
values.put('created_at', getDateTime());
// insert the row
long id = db.insert('users', null, values);
Run Code Online (Sandbox Code Playgroud)

  • @erik SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); formatter.parse(created_at); (2认同)

soo*_*iln 7

您可以使用几种选项:

  1. 您可以尝试使用字符串"(DATETIME('now'))".
  2. 自己插入日期时间,即使用System.currentTimeMillis()
  3. 创建SQLite表时,请将created_date列的默认值指定为当前日期时间.
  4. 使用SQLiteDatabase.execSQL直接插入.


Jar*_*ley 6

对我来说,问题看起来像是"datetime('now')"作为字符串而不是值发送.

我的想法是找到一种方法来获取当前日期/时间并将其作为日期/时间值发送到您的数据库,或者找到一种方法来使用SQLite的内置(DATETIME('NOW'))参数

这个SO.com问题上查看引导者 - 他们可能会引导您朝着正确的方向前进.

希望这有帮助!


Mar*_*lla 5

您可以使用 java 的功能,即:

ContentValues cv = new ContentValues();
cv.put(Constants.DATE, java.lang.System.currentTimeMillis());  
Run Code Online (Sandbox Code Playgroud)

这样,在你的数据库中你保存了一个数字。
这个数字可以这样解释:

    DateFormat dateF = DateFormat.getDateTimeInstance();
    String data = dateF.format(new Date(l));
Run Code Online (Sandbox Code Playgroud)

您应该将数字插入到日期列中,而不是将 l 插入到 new Date(l) 中。
所以,你有你的约会。
例如,我在我的数据库中保存了这个数字:1332342462078
但是当我调用上面的方法时,我得到了这个结果:21-mar-2012 16.07.42