Authentication screen with SQLite

1

I have to perform the authentication of the user, but when I inform the user and the correct password it enters the menu screen, so far so good. The problem is that when I type a bad password or user, I want it to display a user not found message, the problem is that when it is to pop up Toast with the message the app stops.

Bank Search Method

    public Usuario UsuarioAutenticar (String user, String senha){

    Usuario usuario = new Usuario ();

    String autenticarSQL = "SELECT * FROM tblUsuario WHERE usuario = '" + user +"' and senha = '"+senha+"'";

    sqLiteDatabase = bancoDados.getWritableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery(autenticarSQL,null);

    cursor.moveToFirst();

    usuario.setIdUsuario(Integer.parseInt(cursor.getString(0)));
    usuario.setNome(cursor.getString(1).toString());
    usuario.setCpf(cursor.getString(2).toString());
    usuario.setEmail(cursor.getString(3).toString());
    usuario.setTelefone(cursor.getString(4).toString());
    usuario.setUser(cursor.getString(5).toString());
    usuario.setSenha(cursor.getString(6).toString());

    return usuario;
}

Login button method

public void btAcessarOnClick (View view) {

    usuario = usuarioDAO.UsuarioAutenticar(edUsuario.getText().toString(), edSenha.getText().toString());

    if(usuario!=null) {
        Intent intent = new Intent(ActivityLogin.this, ActivityMenu.class);
        startActivity(intent);
    }
    else
        Toast.makeText(this, "Usuario não encontrado", Toast.LENGTH_SHORT).show();
}

Logcat

    
asked by anonymous 17.05.2017 / 01:07

2 answers

2

Hello, Since you have set Usuario usuario = new Usuario (); in the method that authenticates even if it does not exist in bd the return will never be null, it will only be a user object without information ....

It seems that when asking index 0 of the cursor it is giving IndexOutOfBounds because it did not find in the bank .....

try changing from sqLiteDatabase = bancoDados.getWritableDatabase();

for

sqLiteDatabase = bancoDados.getReadableDatabase();

You do not need to change bd, after replacing it in the code:

if(cursor!=null){
    cursor.moveToFirst();
    usuario.setIdUsuario(Integer.parseInt(cursor.getString(0)));
    usuario.setNome(cursor.getString(1).toString());
    usuario.setCpf(cursor.getString(2).toString());
    usuario.setEmail(cursor.getString(3).toString());
    usuario.setTelefone(cursor.getString(4).toString());
    usuario.setUser(cursor.getString(5).toString());
    usuario.setSenha(cursor.getString(6).toString());
}else{
    return null;
}
    
17.05.2017 / 02:27
0

Try this:

    public Usuario UsuarioAutenticar (String user, String senha){
    final SQLiteDatabase db = bancoDados.getReadableDatabase();


    /**
     Aqui você deve colocar todas as colunas que deseja recuperar
     */
    final String[] properties = {"idusuario", "nome", "cpf", "email", "senha", "user", "telefone"};


    /**
     * Vamos criar uma query
     * Informamos:
     * 1. Nome da tabela
     * 2. propriedades que vamos recuperar
     * 3. Condições ( WHERE )
     * 4. Parametros das condições (refere se ao ?) Para cada ? deve haver um parametro neste array
     */
    final Cursor cursor = db.query("tblUsuario", properties, "usuario = ? and senha = ?", new String[]{user, senha}, null, null, null, null);

    /**
     * Este método retornará false se o cursor estiver vazio.
     */
    if(cursor.moveToFirst()){
       final Usuario usuario = new Usuario ();
        usuario.setIdUsuario(Integer.parseInt(cursor.getString(0)));
        usuario.setNome(cursor.getString(1).toString());
        usuario.setCpf(cursor.getString(2).toString());
        usuario.setEmail(cursor.getString(3).toString());
        usuario.setTelefone(cursor.getString(4).toString());
        usuario.setUser(cursor.getString(5).toString());
        usuario.setSenha(cursor.getString(6).toString());

        return usuario;
    }else{
        return null;
    }
}
    
17.05.2017 / 02:31