Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Timestamp column value in android via cursor?

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.

like image 451
Abhishek Avatar asked Dec 27 '11 07:12

Abhishek


People also ask

What is the use of cursor in android?

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.

Which function is used for moving the cursor to the specified row in android?

moveToNext(): Moves the cursor to the next row relative to the current position. boolean Cursor.

Which function do you call from a cursor object to get the number of rows in the cursor object?

getCount. Returns the numbers of rows in the cursor.


1 Answers

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.

like image 104
mchandler Avatar answered Sep 28 '22 03:09

mchandler