Pergunta

I'm performing a query on the mediastore in an effort to return the number of albums for a particular atist.

The query looks like this:

    public String doQuery(String artistName) {

        String[] proj = new String[] { MediaStore.Audio.Artists._ID,
                MediaStore.Audio.Artists.NUMBER_OF_ALBUMS,
                MediaStore.Audio.Artists.ARTIST };

        Uri baseUri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;

        selection = MediaStore.Audio.Artists.ARTIST + " = " + artistName;
        Log.i(TAG, "Selection artistName = " + artistName);

        Cursor mCursor = getContentResolver().query(baseUri, proj, selection,
                null, null);

        mCursor.moveToFirst();

        return

        mCursor.getString(mCursor
                .getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_ALBUMS));

    }

The artistName is passed in, and the selection is supposed to narrow the cursor down to only that artist. I'm getting the following exception:

09-19 23:13:14.855: E/AndroidRuntime(17911): android.database.sqlite.SQLiteException: near "Stone": syntax error (code 1): , while compiling: SELECT _id, number_of_albums, artist FROM artist_info WHERE (artist = Nine Inch Nails)

Can someone please tell me what I'm doing wrong?

Foi útil?

Solução

Do the following:

selection = MediaStore.Audio.Artists.ARTIST + " =? ";
String[] selectionArgs = new String[]{artistName};
Cursor mCursor = getContentResolver().query(baseUri, proj, selection,
                selectionArgs, null);

This way even if artistName contains a single quote, it would execute without error.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top