1

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

2 回答 2

3

我看到额外的空格:

' " + song.getArtistName()+" '

因此,如果您的艺术家名称是“Green Day”,它将变为“Green Day”

相似地:

"= ' " + song.getSongTitle() + "'

所以“碎梦大道”变成了“碎梦大道”

于 2012-10-28T07:33:17.607 回答
0
String selectQuery = "SELECT id FROM " + TABLE_SONG + " WHERE " + SONG_TITLE + "= '" + song.getSongTitle() + "' AND " + ARTIST_NAME + "= '" + song.getArtistName()+"' ";
于 2012-10-28T07:31:37.297 回答