2

My app queries a particular event in CalendarContract.Events using Events._ID. This worked well until attempting to run it on a 5.0 device, now I get an exception

01-12 17:28:50.525: E/Unknown Source(18499): android.database.sqlite.SQLiteException:
    no such column: CalendarContract.Events._ID (code 1): ,
    while compiling:
        SELECT _id, account_type, title, organizer, description, eventLocation,
            hasAlarm, calendar_id
        FROM view_events
        WHERE (lastSynced = 0 AND (CalendarContract.Events._ID=1))

Querying all columns in Events indeed does not return _ID. Any idea why this has been removed or if it's a bug? I can't seem to find away to uniquely identify events any more.

Here is my query:

String[] projection = new String[]{Events._ID, Events.ACCOUNT_TYPE, Events.TITLE,
        Events.ORGANIZER, Events.DESCRIPTION, Events.EVENT_LOCATION, Events.HAS_ALARM,
        Events.CALENDAR_ID};

Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
         "CalendarContract.Events._ID=" + eventId, null, null);

Thanks for any information!

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Karen Forde
  • 1,117
  • 8
  • 20

1 Answers1

2

The answer of @Andrew isn't right. You are wrongly using the selectionClause and the selectionArgs parameters.

Here is what you are doing:

Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
    CalendarContract.Events._ID + "=" + eventId, null, null);

And here is what you should do:

Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
    CalendarContract.Events._ID + " = ?", new String[]{eventId}, null);

The eventId needs to be passed within the selectionArgs so that the contentResolver can build the query statement for you, chaining the selectionArgs and substituting them to the ? char in the selectionClause parameter.

The question is three years old, but I hope my answer can help someone else.

andrea.rinaldi
  • 1,167
  • 1
  • 13
  • 33
  • 1
    Wow, thank you, that fixed it! What's weird is it worked on previous Android versions with the old query. The question is a few months old, not three years ;) Thanks for your help! – Karen Forde Jul 10 '15 at 13:49
  • Glad it helped you :) Maybe on older Android versions there was a bug, I've always been using selectionClause and selectionArgs this way. See ya! – andrea.rinaldi Jul 10 '15 at 16:01