I am making an android database app in which when i fetch values from database using cursor
It gives me the following exception.
java.lang.IllegalStateException: get field slot from row 0 col -1 failed
I am using the following code for fetching:
public Cursor fetchChildren(String KEY_){
Cursor c = db.rawQuery("SELECT children FROM " + DATABASE_TABLE_NAVIGATION
+ " WHERE key_ = ?", new String[] {KEY_});
return c;
}
try{
for(int k=0;k<n;k++){
db.open();
System.out.println("children are");
Cursor cursor=db.fetchChildren(keys_a);
if (cursor.moveToFirst()) // data?
{
System.out.println( cursor.getString(9));
}
}
db.close();
}
catch(Exception e)
{
System.out.println(e);
}
Use
System.out.println( cursor.getString(0))instead ofSystem.out.println( cursor.getString(9));Or use
System.out.println(cursor.getString(cursor.getColumnIndex("children"));Problem in your logic:
You are fetching single column from your data base. So cursor having only one column (children). As you know that column index starts from 0 (0 for first column, 1 for 2nd column …), you have to pass 0 in
getString(). Alternate way is, to search column index in cursor and pass that withgetString(). as getColumnIndex(java.lang.String) calculate the index of column name in cursor, and use it within getString(int).Hope this will help.