Android How to - Check if any rows match the where clause








The following code shows how to check if any rows match the where clause.

Example

//from w  w w . j a v a 2s  .  c om
import org.xmlpull.v1.XmlPullParser;

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();

  /** Checks if any rows match the arguments **/
  public static boolean exists(SQLiteDatabase db, String table, String whereClause, String[] whereArgs) {
    Cursor cursor = db.rawQuery("SELECT 1 FROM " + table + " where " + whereClause, whereArgs);
    boolean exists = cursor.moveToFirst();
    cursor.close();
    return exists;
  }


}