Check if a given string exists in the specified table and column

Description

The following code shows how to check if a given string exists in the specified table and column.

Example


import org.xmlpull.v1.XmlPullParser;
//  w w w. ja  v a  2 s.c  o  m
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 a given string exists in the specified table and column **/
  public static boolean exists(SQLiteDatabase db, String table, String column, String toCheck) {
    String[] selectionArgs = new String[] { toCheck };
    Cursor cursor = db.rawQuery("SELECT 1 FROM " + table + " where " + column + "=?", selectionArgs);
    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