I store a date in my database format “MM/dd/yyyy” and I want to do a query by a specific date but when I make the query the cursor returns nothing.
Is it because of the “/” that is in the string or something else? And yes I know the date is stored properly in the database
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return new CursorLoader(getActivity(),Games.GAMES_URI,new String[] {Games.GAMES_ID},Games.GAMES_DATE + "="+dt,
null,null);
}
converting the date
public convertDate(Calendar date){
mDate = date;
Date d = new Date(date.getTimeInMillis());
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
dt = df.format(d);
}
The query you are trying to do will look like:
where it needs to be:
(assuming that
games_dateis the name of the column — replace as needed)Try
Games.GAMES_DATE + "=?"for your fourthCursorLoaderconstructor parameter, and{dt}for your fifthCursorLoaderconstructor parameter, and Android/SQLite will automatically add your quotation marks for you where needed, assuming that yourContentProvideris backed by SQLite.Also, you might consider storing your date in some other format. If you want it to be a string,
yyyy-MM-dd(oryyyy/MM/dd) is a better choice, as it will sort correctly in chronological order. If you do not need it to be a string, just storinggetTimeInMillis()in anINTEGERcolumn will make it easier to convert to and fromDateobjects without messing with string conversions.