zhaolu 发表于 2016-11-28 12:07:47

SQLite datatime now

  经常需要是插入当前时间,有以下几种方式可以达到目的
  1:建表的时候就设置好了

CREATE TABLE testDate (
id INTEGER PRIMARY KEY AUTOINCREMENT,
t TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
);

  2:利用SQLite内部函数

mDb.execSQL("INSERT INTO "+TABLE+" VALUES (datetime()) ");

  datetime()函数要用引号包起来 当作字符串处理
  3:为什么有以上2种呢,因为直接把获得的date传给SQLite,格式不对,需要format下

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);

 
页: [1]
查看完整版本: SQLite datatime now