Check if any rows match the where clause

Description

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

Example


//from   ww w  .j a  va2s .  co m
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;
  }


}




















Home »
  Android »
    Android Basics »




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