Reputation: 41
I have a function to read data from db.but this always returns error..i dont have much knowledge in android..looking for some help
10-03 11:33:05.870: E/AndroidRuntime(1321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.myworldsafe/com.projects.myworldsafe.DeailedView}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
.
String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+indexd);
return c.getString(index);
}
Upvotes: 0
Views: 270
Reputation: 408
You are getting index out of bounds exception as you are passing number of records in place of column-number. As best coding practices, check cursor for records, i.e. it is empty or contains records. Use this
String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);
if(c.getCount()>0){
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+index);
return c.getString(indexd);
}
else
return "";// when no records found
}
Upvotes: 0
Reputation: 1728
You are probably accessing a 0 row cursor. Change to :
// moveToFirst itself will return false if there is no entry on Cursor
if(c.moveToFirst()){
int cursorCount = c.getCount();
int indexd = c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+index);
// You should getString(ColumnIndex) not CursorCount
return c.getString(indexd);
} else
return "";
Upvotes: 0
Reputation: 1363
Check your database , The query is returning a cursor of size 0 , So you are trying to access cursor at index 0, hence the error. Rethink over the query and check the database for values.
Upvotes: 0
Reputation: 29199
Cursor c=db.rawQuery(selectQuery,params);
is returning cursor with size 0, change your code to check if the cursor has items or not, then move to first position by:
Cursor c=db.rawQuery(selectQuery,params);
if(c.getCount()>0)
{
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+indexd);
return c.getString(index);
}
return "";
Upvotes: 0
Reputation: 30855
here is the right code
int index= c.getCount(); // this will return total rows found
int indexd= c.getColumnIndex(ITEM_CONTENT); // this is the column index by giving the column name.
System.out.print("Count query"+indexd);
return c.getString(indexd); // here you need to pass the column index but you passing the index which was total number of rows
Upvotes: 0
Reputation: 19250
use this:
return c.getString(indexd);
it is what you actually want to get value of. you used index
there,which returns number of record in your cursor.
also better practice is:
String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);
if(c.getCount()>0){
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+index);// use index instead of indexd to print total records in query
return c.getString(index);// return value of column number specified in indexd
}
else
return "";// when no records found
}
Upvotes: 2