Get maximum value in database for given condition

Description

The following code shows to get maximum value in database for given condition.

Example


import org.xmlpull.v1.XmlPullParser;
//  w  w  w . j  a v a2  s.c  om
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class DbUtils {

  private SQLiteDatabase mDb;
  private Context mContext;

  private static final String TAG = DbUtils.class.getSimpleName();
  /** Get maximum value in database for given condition **/
  public static int getMax(SQLiteDatabase db, String table, String column, String whereClause, String[] whereArgs) {
    Cursor cursor = db.query(table, new String[] { column }, whereClause, whereArgs, null, null, column + " ASC");
    if (cursor.moveToFirst()) {
      int maxValue = cursor.getInt(cursor.getColumnIndex(column));
      return maxValue;
    } else {
      cursor.close();
      return -1;
    }
  }

}




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS