Sugar ORM : java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow

94 Views Asked by At

I am using SUGAR ORM, I want to do Sum of column"value" but getting this error.

Please help.

String whereCondition ="Select unique_id, name, Sum(value), type, category_type, note, nutrient_code from NUTRIENTS group by unique_id";

List<Nutrients> nutrientsList = Nutrients.findWithQuery(Nutrients.class, whereCondition);
1

There are 1 best solutions below

0
Prashant Dange On BEST ANSWER

Sum(value) does not work with SugarORM directly. For that we need to use SQL raw query which is as follows:

 String whereCondition ="Select unique_id, name, Sum(value), type, category_type, note, nutrient_code from NUTRIENTS group by unique_id";

    Field f = null;
    try {
        f = SugarContext.getSugarContext().getClass().getDeclaredField("sugarDb");
        f.setAccessible(true);
        SugarDb db = (SugarDb) f.get(SugarContext.getSugarContext());
        Cursor cursor = db.getDB().
                rawQuery(whereCondition, null);