Consult user in Sqlite

Asked

Viewed 612 times

1

I need to validate a user in the database. If valid, I want to call the second Activity.

Which method to use to make this validation in the android database?

Code where the insertion is made:

public String insereProfessor(Professor professor) {
    ContentValues valores;
    long resultado = 1;

    dataBase = bancoDados.getWritableDatabase();
    valores = new ContentValues();
    valores.put("CODPROF", professor.getCodProfessor());
    valores.put("NOME", professor.getNomeProfessor());
    valores.put("USUARIO", professor.getUsuario());
    valores.put("SENHA", professor.getSenha());

    resultado = dataBase.replaceOrThrow("PROFESSOR", null, valores);
    dataBase.close();

    if (resultado == -1)
        return "Erro de registro";
    else
        return "Registro Inserido com sucesso";

}

I tried to authenticate it this way:

public Professor consutaProfessorNoBanco(String usuario, String senha) {

    try {
        String SQL = "select CODPROF, NOME, USUARIO, SENHA from PROFESSOR where USUARIO = ? and SENHA = ? ";
        String[] selectionArgs = new String[]{usuario, senha};
        Cursor cursor = dataBase.rawQuery(SQL, selectionArgs);

        if (cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                professor.setCodProfessor(cursor.getString(cursor.getColumnIndexOrThrow("CODPROF")));
                professor.setNomeProfessor(cursor.getString(cursor.getColumnIndexOrThrow("NOME")));
                professor.setUsuario(cursor.getString(cursor.getColumnIndexOrThrow("USUARIO")));
                professor.setSenha(cursor.getString(cursor.getColumnIndexOrThrow("SENHA")));
            }
        }
        cursor.close();

    } catch (Exception erro) {
        Log.i("Erro ocnsulta", "Erro consultar no banco");
    }

    return professor;
}
  • 2

    What code are you using to create the bank? is some lib, like cupBoard, for example? Without seeing your code we can’t help you.

  • I edited the theme with the code.

  • Where is the code that creates the bank?

  • What do you mean "validate a user in the bank"? Check if it already exists?

No answers

Browser other questions tagged

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