Validate Login with Sqlite

1

I have a problem in the method of validating loguin, it is logging in with both the correct login and password and with them wrong. When it logs in with the wrong login the id of the user goes to 0 and if it logs with the correct data the id goes 1 as a business?

    public Usuario validarLogin(String login, String senha) {

    String[] selectionArgs = new String[]{login, senha};
    Cursor cursor;
    cursor = db.rawQuery("select * from usuarios where login=? and senha=?", selectionArgs);
    Usuario usuarioLinha = new Usuario();
    if (cursor.moveToNext()) {
        usuarioLinha.setId(cursor.getLong(cursor.getColumnIndex("id_usuario")));
        usuarioLinha.setEmail(cursor.getString(cursor.getColumnIndex("email")));
        usuarioLinha.setLogin(cursor.getString(cursor.getColumnIndex("login")));
        usuarioLinha.setSenha(cursor.getString(cursor.getColumnIndex("senha")));
    }
    return usuarioLinha;
}
    
asked by anonymous 13.11.2015 / 01:55

1 answer

1

I have no way of knowing what the post-execution performed after this code, but is probably checking if the user is different from null , I would make the following change:

Usuario usuarioLinha;
    if (cursor.moveToNext()) {
        usuarioLinha = new Usuario()
        usuarioLinha.setId(cursor.getLong(cursor.getColumnIndex("id_usuario")));
        usuarioLinha.setEmail(cursor.getString(cursor.getColumnIndex("email")));
        usuarioLinha.setLogin(cursor.getString(cursor.getColumnIndex("login")));
        usuarioLinha.setSenha(cursor.getString(cursor.getColumnIndex("senha")));
    }
    return usuarioLinha;

Moving the instantiation into the cursor, if db does not return anything, the user will be null , so login is invalid.

    
13.11.2015 / 11:55