Authentication screen with Sqlite

Asked

Viewed 467 times

1

I have to authenticate the user, but when I inform the user and the correct password he enters the menu screen, so far so good. The problem is that when I type an incorrect password or user, I want to present a message user not found, the problem is that when it is to appear Toast with the message the app to.

Search method in the bank

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

Method Login button

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

inserir a descrição da imagem aqui

  • Show the Logcat, please.

  • Follows above in the post

  • CursorIndexOutOfBoundsException: index 0 requested, with a size of 0

2 answers

2

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

It seems that when asking the cursor’s Dice 0 it is giving Indexoutofbounds because it did not find in the bank.....

try to change of sqLiteDatabase = bancoDados.getWritableDatabase();

for

sqLiteDatabase = bancoDados.getReadableDatabase();

because Voce does not need to change the bd, after that replace 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;
}

0

Try it this way:

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

Browser other questions tagged

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