Password validation saved in SQLite

4

Hello, I'm doing a project for college and I'm new to Android, I'm making an application that it's intended to check if the password is correct to finish the application, but I'm not able to validate the password, I do not need validate something else, just the password.

I have created 3 screens one for the user's registration, where his email and password are registered, when he registers he goes to another screen.

The second screen where the password is requested to end the app, and it is on this screen that I need the password to be validated, so that the password that it has registered is the only one that can terminate the app.

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

I'm using this code to register 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 it.

    
asked by anonymous 16.11.2016 / 19:21

1 answer

1

Well if you have to perform password validation with the user's 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 verification as follows:

if (validaSenha(senha, email)){

    //Código para fechar o aplicativo

}

Now if you only want to validate the password, just remove the parameter email from the function and select.

    
30.11.2016 / 19:39