1

I am developing a tutorial project and working with SQLite database. I am trying to retrieve records using WHERE on DATE column in database. But it is retrieving nothing. What is wrong with my code ?

The CREATE statement in database helper class

private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
                                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
                                + COLUMN_DESCRIPTION + " TEXT," 
                                + COLUMN_DATE + " DATE,"
                                + COLUMN_DONE + " BOOLEAN)";

This is how I am retrieving record

String date = "04-22-2014";
ArrayList<Task> tasks = new ArrayList<Task>();
db = getReadableDatabase();
String query = "SELECT * FROM "+TABLE_NAME + " where "+ COLUMN_DATE + " = "+ date;
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst())
{
    do{
        Task task = new Task();
        task.setId(cursor.getInt(0));
        task.setDescription(cursor.getString(1));
        task.setDate(cursor.getString(2));
        task.setDone(Boolean.valueOf(cursor.getString(3)));
        tasks.add(task);
    }
    while(cursor.moveToFirst());
}

return tasks;

But it is not returning any record.

3
  • Make sure you have data inside your database, especially data matching that date. Commented Jan 27, 2016 at 6:48
  • Notice that Sql dates will not be equal if the time is different (i.e the same date but different times). If you want to just compare by date without time, you should cast to date only Commented Jan 27, 2016 at 6:51
  • Sqlite Date data type also store time along with date ? Commented Jan 27, 2016 at 6:55

2 Answers 2

1

To get datas from sqlite database based on Date.You need to use standard format of date while inserting data and retrieving data based on date.The standard date foramt is YYYY-MM-DD. For more info:https://www.sqlite.org/lang_datefunc.html

try this..

String date = "2014-04-22";

instead of

String date = "04-22-2014";

where while saving date in sqlite you need to format the as above mentioned.

Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").

REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.

INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I inserted using that format and retrieving using that format.
use TEXT instead of using DATE .try this.... "CREATE TABLE "+TABLE_NAME+" ("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COLUMN_DESCRIPTION+" TEXT,"+ COLUMN_DATE+" TEXT,"+COLUMN_DONE+" BOOLEAN)";
for supported datatypes in sqlite refer this...sqlite.org/datatype3.html
So it means. Whenever I store date, it is recommanded to use TEXT only ?
0

try this

String query = "SELECT * FROM "+TABLE_NAME + " where "+ COLUMN_DATE + " = "+ "'"+date+"'";

3 Comments

have you checked data in db is it there?
Yes data is inside the db

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.