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 anything.
Please give some hint.
Here is my code for reference.
public List<ObjectiveWiseQuestion> getAllChapter(long subId)
{
List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
String selectQuery=("select chapterName from chapter where subject_id ='"+ subId +"'");
db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst())
{
do {
ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();
owq.setChapterName(cursor.getString(2));
LocwiseProfileList.add(owq);
} while(cursor.moveToNext());
db.close();
}
return LocwiseProfileList;
}
It also shows illegalState Exception.
change:
to this:
and dont forget to close your cursor too right before closing database:
Change your code to:
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