John Ng
John Ng

Reputation: 889

update sqlite column with current date time in android

I have a SQLite table in my android app which has a "MODIFIED" column. It basically stores when (date and time) the current row was first inserted or updated.

If i use update() function of SQLite DB in android, I can't figure out how to put a datetime value in the ContentValues parameter.

If I use execSql() method, i can use the DATETIME() sql function to get the current time is set it to the "MODIFIED" column. But this function does not have a return value. I wouldn't know if the update was successful or not.

Anyone who has any suggestions?

Upvotes: 1

Views: 3472

Answers (2)

Sam
Sam

Reputation: 86948

Another approach is to create a 'TRIGGER' that updates MODIFIED automatically on each 'UPDATE' with SQLite itself, read about it here.

Upvotes: 1

Cristian
Cristian

Reputation: 200090

You just must do something like:

ContentValues values = new ContentValues();
values.put("modified", currentTime);
db.update("table", values, "_id = XXX", null);

Replace _id = XXX with the condition you want to use to update the rows. Update method returns the number of updated rows.

Also, you can use an integer instead of DATETIME (since they actually does not exist in sqlite http://www.sqlite.org/datatype3.html) type for that column and pass System.currentTimeMillis() as currentTime.

Upvotes: 1

Related Questions