如何在Android应用程序中插入date时间设置为“现在”的SQLitelogging?

说,我们有一个表格创build为:

create table notes (_id integer primary key autoincrement, created_date date) 

要插入一条logging,我会用

 ContentValues initialValues = new ContentValues(); initialValues.put("date_created", ""); long rowId = mDb.insert(DATABASE_TABLE, null, initialValues); 

但如何设置date_created列到现在 ? 为了说清楚,

 initialValues.put("date_created", "datetime('now')"); 

是不是正确的解决scheme。 它只是将列设置为“datetime('now')”文本。

您不能使用Java包装器“ContentValues”的date时间函数。 你可以使用:

  • SQLiteDatabase.execSQL,所以你可以input一个原始的SQL查询。

     mDb.execSQL("INSERT INTO "+DATABASE_TABLE+" VALUES (null, datetime()) "); 
  • 还是java的date时间function:

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

在我的代码中,我使用DATETIME DEFAULT CURRENT_TIMESTAMP作为列的types和约束。

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

 create table notes ( _id integer primary key autoincrement, created_date date default CURRENT_DATE ) 

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

方法2

 db.execSQL("INSERT INTO users(username, created_at) VALUES('ravitamada', 'datetime()'"); 

方法3使用javadate函数

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

有几个选项可以使用:

  1. 您可以尝试使用string“(DATETIME('now'))”。
  2. 插入date时间自己,即与System.currentTimeMillis()
  3. 创buildSQLite表时,请将created_date列的默认值指定为当前date时间。
  4. 使用SQLiteDatabase.execSQL直接插入。

对我来说,这个问题看起来像你发送"datetime('now')"作为一个string,而不是一个值。

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

在这个SO.com的问题上检查一下这些服务器 – 他们可能会把你引向正确的方向。

希望这有助于!

你可以使用java的function:

 ContentValues cv = new ContentValues(); cv.put(Constants.DATE, java.lang.System.currentTimeMillis()); 

这样,在你的分贝你保存一个数字。
这个数字可以这样解释:

  DateFormat dateF = DateFormat.getDateTimeInstance(); String data = dateF.format(new Date(l)); 

而不是l到新的date(l)中,您可以将该数字插入到date列中。
所以,你有你的约会。
例如,我保存在我的数据库这个数字:1332342462078
但是当我打电话上面的方法我有这样的结果:2012年3月21日16.07.42

基于@ e-satis的回答,我在我的“DBTools”类上创build了一个私有方法,所以添加当前date现在非常简单:

 ... import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; ... private String getNow(){ // set the format to sql date time SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); return dateFormat.format(date); } ... 

现在像这样使用它: values.put("lastUpdate", getNow());

为我工作完美:

  values.put(DBHelper.COLUMN_RECEIVEDATE, geo.getReceiveDate().getTime()); 

保存你的date。

这个代码示例可以做你想做的事情:

http://androidcookbook.com/Recipe.seam?recipeId=413

请确保该字段没有被标记为“不为空”,同时您尝试使用expression式“(DATETIME('now'))”来插入默认时间戳“

Interesting Posts