Password validation saved in Sqlite

Asked

Viewed 1,422 times

4

Hello I’m doing a project for college and I’m new on Android,I’m making an application that the purpose of it is to check if the password is correct to be able to finalize the application, but I’m not able to validate the password, I do not need to validate anything else, just the password.

I created 3 screens one for user registration, where is registered his email and password, when he registers he goes to another screen.

The second screen where the password is asked to finalize the app, and it is on this screen that I need the password to be validated, so that the password he registered is the only one that can finalize the app.

And the last screen is one for password recovery through email.

I am using this code for registration on the first screen:

    editEmail = (EditText) findViewById(R.id.editEmail); //Determinando o ID das coisas
    editSenha = (EditText) findViewById(R.id.editSenha);

    db=openOrCreateDatabase("CadastroDB", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS cadastro (Email VARCHAR, Senha VARCHAR);");
}

//Botão Cadastrar

public void btnCadastro (View view) {

    if(editEmail.getText().toString().trim().length()==0 || editSenha.getText().toString().trim().length()==0)
    {
        showMessage("Erro", "Preencha os Campos");
    }

    db.execSQL("INSERT INTO cadastro VALUES('"+editEmail.getText()+"','"+editSenha.getText()+"');");
    showMessage("Ok", "Dados Gravados");
    clearText();
    Toast.makeText(getApplicationContext(), "Redirecionando...", Toast.LENGTH_SHORT).show();
    setContentView(R.layout.activity_second);
}

Now I just need to validate on screen 2 this password for the project to end... If anyone can help I would appreciate.

  • Show what you have on the second screen.

1 answer

1


Good if you have to perform password validation with the user email you can use a method that returns if the password and the user exist in the database, for example:

private boolean validaSenha(String senha, String email) {

    // O Cursor recebe o resultado do select
    Cursor cursor = db.execSql("SELECT senha, email FROM cadastro WHERE senha = " + senha + " and email = " + email);

    // Retorna se o cursor tem 1 resultado com o email e senha informados
    return cursor != null && cursor.getCount() == 1;

}

So you can do the check as follows:

if (validaSenha(senha, email)){

    //Código para fechar o aplicativo

}

Now if you want to perform password-only validation, just remove the parameter email function and select.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.