tyczj
tyczj

Reputation: 73826

Query a database with a string that contains "/"

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);
}

Upvotes: 1

Views: 94

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006944

The query you are trying to do will look like:

... WHERE games_date=08/11/2012

where it needs to be:

... WHERE games_date="08/11/2012"

(assuming that games_date is the name of the column -- replace as needed)

Try Games.GAMES_DATE + "=?" for your fourth CursorLoader constructor parameter, and {dt} for your fifth CursorLoader constructor parameter, and Android/SQLite will automatically add your quotation marks for you where needed, assuming that your ContentProvider is 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 (or yyyy/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 storing getTimeInMillis() in an INTEGER column will make it easier to convert to and from Date objects without messing with string conversions.

Upvotes: 1

Related Questions