Search code examples
androidapostrophe

Android : Trouble with query containing apostrophe


I am trying to run a query for my media player as follows:

audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, 
      AlbumColumns.ALBUM + "='" + albumKey + "'", null,AudioColumns.TRACK + " ASC");

A problem arises when the album has an apostrophe in it, as it ends the albumKey segment and errors out.

Does anyone have any suggestions on how to fix?

All help is greatly appreciated.

Thank You, Josh


Solution

  • Use the question mark. You should always do that when using strings.

    audioCursor = getContentResolver().query(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, 
        AlbumColumns.ALBUM + "=?",
        new String[] { albumKey },
        null,
        AudioColumns.TRACK + " ASC");