Monday, 2 July 2012

How to display data between two dates based on android spinners selected item from sqlite db?

 
OK, your YYYY-MM-DD date format looks good; make sure the TO and FROM dates are formatted as strings the same way. The query:




SELECT * FROM TableName where category = ? and dates >= ? and dates <= ?;
or as Pratik suggested


SELECT * FROM TableName where category = ? and dates between ? and ?;
 
As requested, here's a possible SQLiteDatabase query statement:
Cursor cursor = db.query("TableName", null,
                         "category = ? and dates between ? and ?", 
                          new String[] {"personal", "2011-09-07", "2011-09-10"},
                          null, null, null);
Where, naturally, you'd replace the selectionArgs strings with the values from your spinner and FROM and TO dates.
I just noticed that your dates in the table shown do not have leading zeros in the month and day fields; you need these for the comparison to work correctly since you are actually comparing text strings. Note: "2011-09-10" < "2011-9-9" but "2011-09-10" > "2011-09-09"

No comments:

Post a Comment