我想从表KEY_REC_VAL
得到MMDatabaseHelper.TABLE_RECORD
的总和。我的代码导致致命错误,我不知道如何解决它。有任何想法吗?谢谢。
public Integer getIncomesSum()
{
String col = MMDatabaseHelper.TABLE_RECORD+"."+MMDatabaseHelper.KEY_REC_VAL;
String whereClause = " WHERE " +TABLE_RECORD+"."+KEY_REC_VAL+" >"+" 0";
String query = "SELECT sum ("+col+
") FROM "+TABLE_RECORD+
whereClause;
Log.i("SQL",query);
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery(query,null);
} catch (SQLiteException e) {
} finally {
} return cursor.getInt(0);
这是错误:
ERROR: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
答案 0 :(得分:2)
添加此行cursor=cursor.moveToFirst();