How do I get the Timestamp value from database column via a cursor and store it in a Timestamp variable in android sdk?
I'm using the java.sql.Timestamp
and the value in the sqlite database is of type TIMESTAMP.
I have created an class having all the fields as the database column names and wish to create a new object after reading a record from the database.
Introduction to Cursor in Android The basic purpose of a cursor is to point to a single row of the result fetched by the query. We load the row pointed by the cursor object. By using cursor we can save lot of ram and memory. Here, we pass the table name, column name only then we receive the cursor.
moveToNext(): Moves the cursor to the next row relative to the current position. boolean Cursor.
getCount. Returns the numbers of rows in the cursor.
You have to use the Cursor methods to get the value as a string and then use the Timestamp class's static valueOf method to convert it back to a proper Timestamp:
Timestamp.valueOf(cursor.getString(0))
I use this all over the place and it works like a charm. For example, while iterating over the result set, you can set the Timestamp fields of your object.
obj.setId(cursor.getLong(0));
obj.setName(cursor.getName(1));
obj.setAddDate(Timestamp.valueOf(cursor.getString(2)));
I hope that's what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With