我有一个获取歌曲id的函数。 id是主要字段和自动增量。但不知何故,每当我调用此函数时,它返回-1,结果未找到。我如何处理这个问题有什么问题吗?
int getIdForSong(Song song){
String selectQuery = "SELECT id FROM " + TABLE_SONG + " WHERE " + SONG_TITLE + "= ' " + song.getSongTitle() + "' AND " + ARTIST_NAME + "= ' " + song.getArtistName()+" ' ";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if(cursor.moveToNext() && cursor != null){
int id = Integer.parseInt(cursor.getString(0));
return id;
}
else
return -1;
}
答案 0 :(得分:3)
我看到了额外的空格:
' " + song.getArtistName()+" '
因此,如果您的艺术家名称是“绿日”,那么它将成为“绿日”
类似地:
"= ' " + song.getSongTitle() + "'
所以“破碎的梦想大道”变成了“破碎的梦想大道”
答案 1 :(得分:0)
String selectQuery = "SELECT id FROM " + TABLE_SONG + " WHERE " + SONG_TITLE + "= '" + song.getSongTitle() + "' AND " + ARTIST_NAME + "= '" + song.getArtistName()+"' ";