samedi 12 mars 2016

how to fetch today,last week ,last month rows from sqlite android

I have a table contains 5 columns one of them is purchase_date and the date stored in sqlite as string in the form of ("yyyy-mm-dd") .I have 3 buttons and i want to retrieve rows inserted today,last week, last 30 days on button click .

for today and last week i tried this and it didn't work

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String currentDate = sdf.format(new Date());

    String query = "select * from " + Purchase_TABLE_NAME + " where " + Purchase_COLUMN_DATE + " <= " + currentDate;
    Cursor cursor = db.rawQuery(query, null);
    return cursor;

and for last week

String query = "select * from " + Purchase_TABLE_NAME + " where " + Purchase_COLUMN_DATE+ " <= date('now','localtime', '-7 day')" ; Cursor cursor = db.rawQuery(query, null); return cursor;

any help appreciated and thanks in advance.

Aucun commentaire:

Enregistrer un commentaire