Authenticate user with SQLITE database

Asked

Viewed 158 times

0

I’m needing the parameters used in the button to query the database (the code was the result of another post response) when trying to implement the function inside the baton I could not get it to work.

// FUNÇÃO TESTE LOGIN 1
public void UsuarioAutenticar (int id, String user, String senha) {
    LoginModelo usuario = new LoginModelo();
    String selectQuery = "SELECT * FROM TBL_USRS WHERE KEY_USER = '" + user + "' and KEY_SENHA = '" + senha + "'";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor != null) {
        //final Usuario usuario = new Usuario ();
        cursor.moveToFirst();
        usuario.setId(Integer.parseInt(cursor.getString(0)));
        usuario.setNome(cursor.getString(1).toString());
        usuario.setSenha(cursor.getString(6).toString());
    }
}// FIM FUNÇÃO TESTE LOGIN 1

// função teste login 2
public LoginModelo UsuarioAutenticado (String user, String senha){
    final SQLiteDatabase db = this.getReadableDatabase();
    /**
     Aqui você deve colocar todas as colunas que deseja recuperar
     */
    final String[] properties = {"id", "nome", "senha"};
    final Cursor cursor = db.query("TBL_USRS", properties, "KEY_USER = ? and KEY_SENHA = ?", new String[]{user, senha}, null, null, null, null);
    /**
     * Este método retornará false se o cursor estiver vazio.
     */
    if(cursor.moveToFirst()){
        final LoginModelo usuario = new LoginModelo ();
        usuario.setId(Integer.parseInt(cursor.getString(0)));
        usuario.setNome(cursor.getString(1).toString());
        usuario.setSenha(cursor.getString(6).toString());
        return usuario;
    }else{
        return null;
    }
} // FIM FUNÇÃO TESTE LOGIN 2

tried so

checkLOGON.setOnClickListener(new View.OnClickListener() {
      @Override
        public void onClick(View v) {
          //Resgatando valores dos edittext
          EditText checkNome = findViewById(R.id.user);
          EditText checkSenha = findViewById(R.id.login);
          //convertendo em strings
          String checkNomes = checkNome.getText().toString();
          String checkSenhas = checkSenha.getText().toString();
          // comparando os campos digitados com os da variavel predefinida
          //checarLogin
          String usuario;
          usuario = databaseHelper.UsuarioAutenticado( int id, user, senha);
              if (usuario != null) {
                  Intent intent = new Intent(MasterLogin.this, MainActivity.class);
                  startActivity(intent);
              } else
                  Toast.makeText(MasterLogin.this, "Usuario não encontrado", Toast.LENGTH_SHORT).show();
          }

and so:

LoginModelo = databaseHelper.UsuarioAutenticado(int id, user , senha);
        if(LoginModelo!=null) {
              Intent intent = new Intent(MasterLogin.this, MainActivity.class);
              startActivity(intent);
          }else
              Toast.makeText(MasterLogin.this, "Usuario não encontrado", Toast.LENGTH_SHORT).show();
      }

but I understand it’s not right I tested this line as suggested:

usuario = databaseHelper.UsuarioAutenticar( int id, checkNome.getText().toString(),checkSenha.getText().toString());

and

usuario = databaseHelper.UsuarioAutenticar( checkNomes,checkSenhas);

Obs: I tested with the two functions select Authenticate User and Authenticated User.

I couldn’t reach a conclusion about my mistakes in this verification step for login.

  • 1

    Which error appears?

  • '.class' expected

  • '.class' expected ';' expected

  • when I use the second code it returns: ...""(java.lang.String, java.lang.String)' on a null Object Reference

  • it seems that I was making a brief confusion between builder and class. now appear other errors. E/Spannablestringbuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

  • I watched the error logs and went through one by one until I completed the flaws in my failed code.

Show 1 more comment

1 answer

0

First I called the database, whose function of selecting the records was contained in the code and without which it would do nothing

private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    databaseHelper = new DatabaseHelper(this);

   final Button checkLOGON = findViewById(R.id.btnLOGON);
    checkLOGON.setOnClickListener(new View.OnClickListener() {
      @Override
        public void onClick(View v) {
          //Resgatando valores dos edittext
          EditText checkNome = findViewById(R.id.user);
          EditText checkSenha = findViewById(R.id.login);
          //convertendo em strings
          String checkNomes = checkNome.getText().toString();
          String checkSenhas = checkSenha.getText().toString();

          // INCIALIZA AVERIGUAÇÃO SE O USUÁRIO E SENHA EXISTEM
          LoginModelo loginModelo = databaseHelper.UsuarioAutenticado( checkNome.getText().toString(), checkSenha.getText().toString()); // nome , senha
          if (loginModelo != null) {
              Toast.makeText(Login.this, "Bem vindo: " + checkNomes , Toast.LENGTH_SHORT).show();
              Intent intent = new Intent(Login.this,MainActivity.class);
              startActivity(intent);
            } else

            Toast.makeText(Login.this, "Confimarção de Usuário e/ou Senha não conferem! \n Você informou: \n Nome "+ checkNomes + " Senha " + checkSenhas, Toast.LENGTH_SHORT).show();
      } //fim on click View v
 }); // fim checkUserSenha

} // fim bundle

Browser other questions tagged

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