I have a query in my database like this:
Cursor c= ourDB.rawQuery("SELECT SUM(odometer) AS odometer FROM tripmileagetable WHERE car='abdul' AND date LIKE '2012-07%'", null);
if(c.getCount()<=0){
return 0;
}else{
int sum= c.getInt(0);
Log.i("Sum of odometer", ""+sum);
return sum;
}
and its giving me this exception. If I write this in the else section:
int sum= c.getColumnIndex("odometer");
Log.i("Sum of odometer", ""+sum);
return sum;
then this returns 0, but when I put 0 it gives me the exception, I am unable to find out my fault. Can any one help?
You missed out
c.moveToFirst();that is why your cursor is giving exception as by default it points index -1.