我正在对媒体商店执行查询,以便返回特定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));
}
传入artistName,选择应该将光标缩小到只有该艺术家。我遇到以下异常:
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)
有人可以告诉我我做错了吗?
答案 0 :(得分:3)
执行以下操作:
selection = MediaStore.Audio.Artists.ARTIST + " =? ";
String[] selectionArgs = new String[]{artistName};
Cursor mCursor = getContentResolver().query(baseUri, proj, selection,
selectionArgs, null);
这种方式即使artistName
包含单引号,它也会毫无错误地执行。