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;
}
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.
– Leonardo Dias
I edited the theme with the code.
– alannrs
Where is the code that creates the bank?
– viana
What do you mean "validate a user in the bank"? Check if it already exists?
– Jorge B.