我试图防止将用户双重插入到数据库中,并且每次收到消息时都检查电子邮件:
my email already exists
谢谢回答。
检查是否userEmail已经存在:DataBaseHelper类:
public boolean checkIfExists(String userEmail){
db = this.getReadableDatabase();
String query = "select "+ COL_EMAIL + " from " +TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String existEmail;
if (cursor.moveToFirst()) {
do {
existEmail = cursor.getString(0);
if (existEmail.equals(userEmail)) {
return true;
}
} while (cursor.moveToNext());
}
return false;
}
验证电子邮件,检查电子邮件是否已经存在:Registration类:
private boolean validateEmail() {
String emailInput = textInputEmail.getEditText().getText().toString().trim();
if (myDb.checkIfExists(emailInput)) {
textInputEmail.setError("Email already exist");
return false;
} else if (emailInput.isEmpty()) {
textInputEmail.setError("Field can't be empty");
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(emailInput).matches()) {
textInputEmail.setError("Please enter a valid email address");
return false;
} else {
textInputEmail.setError(null);
return true;
}
}
相关分类