Android SQLite开发:两表连接后分别求和两列并计算商
在Android中同一函数实现两列求和及除法计算
嘿,早上好呀!针对你在Android项目里的需求,我给你整理了两种可行的方案,你可以根据自己的场景来选:
方案一:遍历查询到的列表计算总和
既然你已经通过rawQuery获取到了Courses列表,那可以直接在遍历列表的同时累加两列的值,之后再做除法运算。这种方式不需要额外的数据库查询,适合数据量不大的场景:
public List<Courses> getListCoursesWithCalculation(int id) { Courses courses = null; List<Courses> coursesList = new ArrayList<>(); // 初始化两个总和变量,根据列类型选int/long/double double column1Total = 0.0; double column2Total = 0.0; openDatabase(); // 替换成你实际的联表SQL语句 Cursor cursor = mDatabase.rawQuery("SELECT * FROM table1 JOIN table2 ON table1.id = table2.id WHERE table1.id = ?", new String[]{String.valueOf(id)}); if (cursor.moveToFirst()) { do { // 替换成你实际的列名 double column1Value = cursor.getDouble(cursor.getColumnIndexOrThrow("target_column1")); double column2Value = cursor.getDouble(cursor.getColumnIndexOrThrow("target_column2")); // 累加总和 column1Total += column1Value; column2Total += column2Value; // 构建Courses对象并加入列表(和你原有逻辑一致) courses = new Courses(); courses.setId(cursor.getInt(cursor.getColumnIndexOrThrow("id"))); // 其他属性赋值... coursesList.add(courses); } while (cursor.moveToNext()); } cursor.close(); closeDatabase(); // 计算除法,必须处理除数为0的情况,避免崩溃 double divisionResult = 0.0; if (column2Total != 0) { divisionResult = column1Total / column2Total; } else { // 除数为0的自定义处理逻辑,比如打日志或给默认值 Log.w("CalcWarning", "第二列总和为0,无法执行除法"); } // 这里可以把结果返回/存储,根据你的业务需求调整 Log.d("CalcResult", "列1总和:" + column1Total + ",列2总和:" + column2Total + ",除法结果:" + divisionResult); return coursesList; }
方案二:使用SQL聚合查询直接获取总和
如果数据量较大,或者不想遍历列表,直接用SQL的SUM()函数让数据库层面计算总和,效率会更高:
public List<Courses> getListCoursesWithCalculation(int id) { Courses courses = null; List<Courses> coursesList = new ArrayList<>(); double column1Total = 0.0; double column2Total = 0.0; openDatabase(); // 第一步:查询列表数据(和你原有逻辑一致) Cursor listCursor = mDatabase.rawQuery("SELECT * FROM table1 JOIN table2 ON table1.id = table2.id WHERE table1.id = ?", new String[]{String.valueOf(id)}); if (listCursor.moveToFirst()) { do { courses = new Courses(); courses.setId(listCursor.getInt(listCursor.getColumnIndexOrThrow("id"))); // 其他属性赋值... coursesList.add(courses); } while (listCursor.moveToNext()); } listCursor.close(); // 第二步:执行聚合查询获取两列总和 Cursor sumCursor = mDatabase.rawQuery("SELECT SUM(target_column1) as total1, SUM(target_column2) as total2 FROM table1 JOIN table2 ON table1.id = table2.id WHERE table1.id = ?", new String[]{String.valueOf(id)}); if (sumCursor.moveToFirst()) { column1Total = sumCursor.getDouble(sumCursor.getColumnIndexOrThrow("total1")); column2Total = sumCursor.getDouble(sumCursor.getColumnIndexOrThrow("total2")); } sumCursor.close(); closeDatabase(); // 处理除法运算 double divisionResult = 0.0; if (column2Total != 0) { divisionResult = column1Total / column2Total; } else { Log.w("CalcWarning", "第二列总和为0,无法执行除法"); } Log.d("CalcResult", "列1总和:" + column1Total + ",列2总和:" + column2Total + ",除法结果:" + divisionResult); return coursesList; }
关键注意事项:
- 替换代码中的
table1、table2、target_column1、target_column2为你实际的表名和列名。 - 若列是整数类型,把
double换成int或long,除法时要注意整数精度问题(比如用(double)column1Total / column2Total得到浮点数结果)。 - 务必处理除数为0的情况,否则会抛出
ArithmeticException导致APP崩溃。 - 使用完
Cursor后一定要调用close(),避免内存泄漏。
内容的提问来源于stack exchange,提问作者user7197369
相关产品推荐
相关产品推荐

