Skip to content Skip to sidebar Skip to footer

Android Sql: Check If Record In Database Exists

I am trying to query my database based on a specific id. String sql = 'SELECT * FROM mash WHERE recipe_id = '' + id + '''; Cursor data = database.rawQuery(sql, null); If this is t

Solution 1:

if (cursor.moveToFirst()) {
    // record exists
} else {
    // record not found
}

Solution 2:

My Code is:

public void addContact(String a, String b, String c, String d, SQLiteDatabase db)
    {
        //cursor is use to check if data is availble then show row is exist if not then execute insert query.
        Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS + " WHERE    email=? ", new String[]{b});

        if (mCursor.moveToFirst())
        {
            value=false;
            Toast.makeText(context,"Email alreay exist please enter anothe email id",Toast.LENGTH_LONG).show();
        }
        else
        {
            ContentValues values = new ContentValues();

            values.put(Database_Handler.NAME, a); // Contact Name
            values.put(Database_Handler.EMAIL, b);
            values.put(Database_Handler.PASSWORD, c);
            values.put(Database_Handler.NUMBER, d);

            db.insert(Database_Handler.TABLE_CONTACTS, null, values);
            Log.e("DATABASE OPERATION", "One row is insert");

            Toast.makeText(context,"Data insert Sucessfully",Toast.LENGTH_SHORT).show();
            db.close();
            value=true;

        }
    }

Post a Comment for "Android Sql: Check If Record In Database Exists"