How To Retrieve Value From The Sqlite Database?
I have a two tables that are Subject and chapter. I need to display chapter_name by passing Subject_id. My question is that how to do that? When I pass value id doesn't return anyt
Solution 1:
Change your code to:
publicList<ObjectiveWiseQuestion> getAllChapter(long subId)
{
List<ObjectiveWiseQuestion>LocwiseProfileList=newArrayList<ObjectiveWiseQuestion>();
String selectQuery=("select chapterName from chapter where subject_id =?");
db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, newString[]{subId}); // The secure way of executing raw queriesif (cursor.moveToFirst())
{
do {
ObjectiveWiseQuestion owq= newObjectiveWiseQuestion();
//Get column index like this
owq.setChapterName(cursor.getString(cursor.getColumnIndexOrThrow("subject_id")));
LocwiseProfileList.add(owq);
} while(cursor.moveToNext());
cursor.close();
db.close();
}
returnLocwiseProfileList;
}
I have changed you query to use the secure way.
Also, cursor.getString(0) or cursor.getString(2) should not be used . Beacause sqlite documentation states that it is not necessary that the query result would have the columns in the same order as in the time of table of creation. This would give you error sometimes but not everytime
Solution 2:
change:
owq.setChapterName(cursor.getString(2));
to this:
/* read value from first column (chapterName) i.e. at index 0 */
owq.setChapterName(cursor.getString(0));
and dont forget to close your cursor too right before closing database:
cursor.close();
db.close();
Post a Comment for "How To Retrieve Value From The Sqlite Database?"