Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to add a last modified timestamp while using ORMLite and Android?

I want to add a field indicating the last modified or last accessed time for my objects. Is there an easy, auto refreshed method for doing so?

like image 937
Quentamia Avatar asked Apr 24 '12 15:04

Quentamia


1 Answers

I'm not sure about how to use SQLite to support a last-modified timestamp. However, one of the features of ORMLite is a version field. You can mark a Date field with version = true and ever time it is updated it will update the Date to the latest value. See the documentation for more information:

http://ormlite.com/docs/version-field

So you would add something like the following to your class:

@DatabaseField(version = true)
Date modificationDate;

I've verified this works well both for the creation and modification dates. Also, this means that when you do any upgrades, it will add something like the following to the end of the update statement automagically:

UPDATE yourclass SET ... >>> WHERE modificationDate = existing-date; <<<

See the docs for more information. Oh, and if you'd like to store it as a long, instead you could do something like:

@DatabaseField(version = true, dataType = DataType.DATE_LONG)
Date modificationDate;
like image 140
Gray Avatar answered Nov 15 '22 08:11

Gray