androidandroid-studiosugarorm

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


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);

Solution

  • 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);