android SUM中的SQLlite选择不正常

时间:2016-06-02 16:58:05

标签: android sqlite

我想从表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

1 个答案:

答案 0 :(得分:2)

在返回语句之前

添加此行cursor=cursor.moveToFirst();