2012-10-27 73 views
2
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); 
    int id = Integer.parseInt(cursor.getString(0)); 
    return id; 
} 

我越來越接近「=」例外。任何人?以下selectQuery有什麼問題嗎?

+1

你已經給你的' 「=」'取代' 「=」「'看看[this](http://www.vogella.com/articles/AndroidSQLite/article.html#sqliteoverview_rawquery) – Praveenkumar

+0

也int id = Integer.parseInt(cursor.getString(0));拋出CursorOutOfBoundIndex ..我不知道爲什麼,因爲ID應該返回在getString(0)作爲id字段是主鍵自動增量。 – Saurabh

回答

1

變化selectQuery值:

"SELECT id FROM " + TABLE_SONG + " WHERE " + SONG_TITLE + " = '" + song.getSongTitle() + "' AND '" + ARTIST_NAME + "' = '" + song.getArtistName() + "'"; 
+0

無論你發現,添加雙引號和單引號 – RajeshVijayakumar

0

嘗試此

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); 
int id = Integer.parseInt(cursor.getString(0)); 
return id; 
} 
+0

int id = Integer.parseInt(cursor.getString(0));也扔CursorOutOfBoundindex ...我不知道爲什麼,因爲getString(0)應該返回第一個字段ID,我已經保持小學和自動增量 – Saurabh

0

對於CursorOutOfBoundException,

光標光標= db.rawQuery後(selectQuery,NULL); ,

如果(空!=光標& & cursor.moveToFirst()){

int id = Integer.parseInt(cursor.getString(0)); 
    return id; 

}

+0

Thanx很多,但你能告訴我爲什麼會發生這種情況? – Saurabh