I want to check if records exist in the SQLite database, here\'s what I have done so far. when I search for a already existing records im getting value from EditText in list.
I use this code in my application and it works perfact...
LoginActivity.java
DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());
String email_id = email.getText().toString();
boolean dbHelper.isExist(email_id);
// if record is exist then it will return true otherwise this method returns false
Using rawQuery
public boolean isExist(String strEmailAdd) {
db = this.getReadableDatabase();
cur = db.rawQuery("SELECT * FROM " + USER_TABLE + " WHERE email_id = '" + strEmailAdd + "'", null);
boolean exist = (cur.getCount() > 0);
cur.close();
db.close();
return exist;
}
Using db.query
public boolean isExist(String strEmailAdd){
String whereClause = "email_id = ?";
String[] whereArgs = new String[]{strEmailAdd};
db = database.getWritableDatabase();
cur = db.query(USER_TABLE, null, whereClause, whereArgs, null, null, null);
boolean exist = (cur.getCount() > 0);
cur.close();
db.close();
return exist;
}