Froyo and Calendar

I had application on market for quite a while and suddenly I started receiving "SQLiteException". Quick look at stack trace showed little bit more details:

android.database.sqlite.SQLiteException: no such column: Calendars._id: , while compiling: SELECT _id, title, dtstart FROM view_events WHERE (1) AND (Calendars._id=1)

This clearly pointed toward code that fills calendar events. Generally it looked like this:

Uri calendarsUri;
Uri eventsUri;
if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1
calendarsUri = Uri.parse("content://calendar/calendars");
eventsUri = Uri.parse("content://calendar/events");
} else { //Android 2.2 (Froyo) and later
calendarsUri = Uri.parse("content://com.android.calendar/calendars");
eventsUri = Uri.parse("content://com.android.calendar/events");
}
...
Cursor cursor = resolver.query(eventsUri, new String[]{ "_id", "title", "dtstart" }, "Calendars._id=" + calendarId, null, null);
...

Exception pointed right to that last line with query. Froyo had no idea what it should do with Calendars._id.

To make long ranting short, correct code should be:

Cursor cursor;
if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1
cursor = resolver.query(eventsUri, new String[]{ "_id", "title", "dtstart" }, "Calendars._id=" + calendarId, null, null);
} else {
cursor = resolver.query(eventsUri, new String[]{ "_id", "title", "dtstart" }, "calendar_id=" + calendarId, null, null);
}

With this everything started working again and, since calendar code got first-class citizen treatment and it is finally documented in SDK, I doubt that there will be further breaking changes down the road.

2 thoughts to “Froyo and Calendar”

Leave a Reply

Your email address will not be published. Required fields are marked *