2012-10-27 3 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 throw합니다. ID 필드를 기본 키 autoincrement로 ID를 getString (0) 반환해야하는 이유를 모르겠습니다. – 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)은 기본 및 autoincrement를 유지 한 첫 번째 필드 ID를 반환해야하기 때문에 이유를 모르겠다 – Saurabh

0
CursorOutOfBoundException를 들어

, 커서 커서 = db.rawQuery 후

(selectQuery, NULL); ,

경우 (NULL! = 커서 & & cursor.moveToFirst()) {

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

} 당신은`당신` "="`대신` "= '"부여했습니다

+0

고맙습니다. 왜 이런 일이 정확히 일어 났습니까 ?? – Saurabh

관련 문제