SQLite how to sum column values ​​in `GridView`

I looked around and found several similar cases, but not where there is a need for specificity in the amounts. So here I am.

I have a method filterPayments

that returns all records in mine PayTable

based on a specific one GroupID

and then displays in mine GridView

. From there I want to sum the values ​​of 2 out of 5 columns in PayTable

, especially my columns Interest

and Due

. I'm not sure how to do this in a query, let alone this, only for certain columns.

Question

How to add the values ​​of all records in a specific column.

Is it possible to do this in a SQLite query? If so , how can I use the return value filterPayments

and only do the sum over certain columns? If it is not , how can I do it?

Below are my code snippets.

filterPayments

Cursor filterPayments(String Payment) {
    SQLiteDatabase db = this.getWritableDatabase();
    String[] columns = new String[]{"_id", colGroupID, colPayBal, colInterest, colDue, colDateDue, colPaid};
    Cursor c = db.query(viewPmnts, columns, colGroupID + "=?", new String[]{Payment}, null, null, null);
    return c;
}

      

GridView

public void Paygrid() {

    dbHelper = new DatabaseHelper(this);
    String Payment = String.valueOf(txt.getText());
    Cursor a = dbHelper.filterPayments(Payment);
    startManagingCursor(a);

    String[] from = new String[]{DatabaseHelper.colPayBal, DatabaseHelper.colInterest, DatabaseHelper.colDue, DatabaseHelper.colDateDue, DatabaseHelper.colPaid};
    int[] to = new int[]{R.id.Amount, R.id.Interest, R.id.Due, R.id.DateDue, R.id.Paid};

    SimpleCursorAdapter saa = new SimpleCursorAdapter(this, R.layout.paygrid, a, from, to);
    intgrid.setAdapter(saa);
}

      

0


source to share


1 answer


I suggest pulling all data from a column and then summarizing it in Java or Android. This would be the easiest way.

There are no core sqlite functions that do this. https://www.sqlite.org/lang_corefunc.html



However, you can create custom sqlite functions. See below. How to create custom functions in SQLite

+1


source







All Articles