Sugar ORM:java.lang.IllegalStateException:无法从CursorWindow读取行0,col -1

时间:2018-10-23 13:54:18

标签: android android-studio sugarorm

我正在使用SUGAR ORM,我想做“值”列的总和,但遇到此错误

请帮助。

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 个答案:

答案 0 :(得分:0)

Sum(value)不能直接与SugarORM一起使用。为此,我们需要使用如下的SQL原始查询:

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