Skip to content Skip to sidebar Skip to footer

Sqlite: Obtaining Total Of All Values In One Column

I want to create a Query that will give the total of all values in a single column of my SQLite datase. I want this query to be within a method that returns the total as an int so

Solution 1:

Read the documentation; you can use either sum() or total().

The name of the column is not COL_MED; that is the name of the symbol whose value is the name of the column. Like TABLE_SCORE, you would have to insert its value into the SQL query:

intgetSumOfAllAvgMeditations() {
    SQLiteDatabasedb= getWritableDatabase();
    try {
        Stringsql="SELECT TOTAL(" + COL_MED + ") FROM " + TABLE_SCORE;
        return (int)DatabaseUtils.longForQuery(db, sql, null);
    } finally {
        db.close();
    }
}

Post a Comment for "Sqlite: Obtaining Total Of All Values In One Column"