Can't verify user login details in Android SQLite

Here is the code I used to check if a user is already the part of database or not:

  Boolean checkEmailPass = db.emailpasswd(email, pass);
            if (checkEmailPass== false){
                Toast.makeText(getApplicationContext(),"Email or password incorrect", Toast
                        .LENGTH_SHORT).show();
            }else{
                Intent i = new Intent(LoginActivity.this, HomeActivity.class);
                startActivity(i);
                finish();

                Toast.makeText(getApplicationContext(),"Successful Login",Toast.LENGTH_SHORT).show();
            }

Below is the code part from datahelper class. The name of the table is user.

 public boolean emailpasswd(String email, String password){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("Select * from user where email=? and password = ?", new
            String[]{email, password});
    if (cursor.getCount()>0){
        return false;
    }else {
        return true;
    }
  }
}

I have created an application with the help of a SQLite database. The whole application is working fine, but just whenever I am trying to log in, the application is not able to check email and password from the database and that's why no matter what I enter I get the toast of successful login. Please check the code and guide me through.

1 answer

  • answered 2018-07-11 07:21 mTak

    Your code:

    if (cursor.getCount()>0){
        return false;
    }else {
        return true;
    }
    

    So if the user exists you return false!!! Change it the other way:

    if (cursor.getCount()>0){
        return true;
    }else {
        return false;
    }
    

    and put a db.close() at the end.