views:

113

answers:

1

Hi all,

in Android is it possible to insert a sqlite timestamp into a database using ContentValues?

When I try to add it using something like

ContentValues args = new ContentValues();       
 args.put(MY_DATE,my_date);

I get an error telling me that my_date needs to be a string

+3  A: 

Neither Date nor Calendar are valid things to put in a ContentValues. The most efficient format, I think, is to convert the Date to milliseconds (getTime()) and store that in an INTEGER column.

CommonsWare
Note though, you can't use getInt() when retrieving the data, you need to use getDouble() (on the cursor). Java-ints arn't big enough to store timestamps (this game me a major headace a while ago).
Alxandr
Or just use getLong().
alexanderblom
Thanks for the responses. It seems that I can add the timestamp to the contentvalues as a string and that it inserts into the database as an sql timestamp, when I use mDb.insert(DATABASE_TABLE, null, args), no problem.
Kevin Bradshaw