0

我正在对 mediastore 执行查询,以返回特定 atist 的专辑数量。

查询如下所示:

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

    }

艺术家名称被传入,并且选择应该将光标缩小到仅该艺术家。我收到以下异常:

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)

有人可以告诉我我做错了什么吗?

4

1 回答 1

3

请执行下列操作:

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

这样即使artistName包含单引号,它也会执行而不会出错。

于 2012-09-19T13:29:01.997 回答