我有一个获取歌曲 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;
}