views:

19

answers:

1

Hi!

I am new to android. I am using a database to store my data. I also store date as TEXT in database. format of date is as follow... "Mon, 20 Oct 2010 12:30:00" So i stored them in TEXT datatype not in DateTime. Now i want to display them in following format "Mon, 20 Oct 2010" The main thing is that i want to ORDER them in DESC. I write a query for this but it dose not work. It there is some alternative for this???? i also read about "strftime()" function of SQLITE but i did not know how to use it for this.

My Query is:

"Cursor c = myDB.rawQuery("Select * from List ORDER BY to_date(substr(date,6,20), 'DD-MM-YYYYTHH24:MI:SS') DESC",null);"

Please Help me... Thanks in advance!

+1  A: 

I'd recommend you to store your DateTime values as INTEGER using Date.getTime() (milliseconds since Jan. 1, 1970, midnight GMT). So you can use ASC and DESC operators without conversions which will require less resources.

cement
So in such a case i have to store my date in 2 columns. It is right?
Arslan
Can any function of sqlite is not going to help me????
Arslan
Why do you need 2 columns? 1 will be enough.
cement
But i am storing whole date as TEXT because need this whole date for display to user, but in such a case it must be sorted in DESC... For 1 column for whole date and 1 for TIME as INTEGER?????
Arslan
No. Date.getTime() converts both date and time to into single integer value. Same way you can convert it back to Date after reading from database and format it as needed.
cement
Oh! Thanks. Can you please send me a example or a useful link?
Arslan
Date.getTime() is a java funtion? or a sqlite function?
Arslan
java.util.Date. However I believe SQL type has similar functionality.
cement
It is a java function that converts java.util.Date to long that can be stored as INTEGER in SQLite database.
cement