Check if record exist in the database android

后端 未结 3 436
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 03:53

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.

3条回答
  •  不思量自难忘°
    2021-01-23 04:18

    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;
    }
    

提交回复
热议问题