我添加了一种方法来检查数据库中用户电子邮件的存在。例如,现有用户不能使用同一电子邮件再次注册帐户。但是我仍然可以使用相同的电子邮件注册我的帐户。我的代码没有错误。我不知道问题出在哪里。大家都可以帮我解决这个问题吗?
这是我在DatabaseHelper.JAVA中添加的方法
public boolean userExists (String email){
String [] columns = {C_EMAIL};
SQLiteDatabase db = getReadableDatabase();
String selection = C_EMAIL + "=?";
String selectionArgs []= { email };
Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
return true;
}
这是我的MainActivity.Java
private void getData() {
email = "" + Pemail.getText().toString().trim();
name = "" + Pname.getText().toString().trim();
age = "" + PAge.getText().toString().trim();
phone = "" + Pphone.getText().toString().trim();
preferenceselected = "" + Ppreferenceselected.getText().toString().trim();
password = "" + Ppassword.getText().toString().trim();
if (email.isEmpty() || name.isEmpty() || age.isEmpty() || phone.isEmpty() || preferenceselected.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please fill all the information", Toast.LENGTH_SHORT).show();
if (dbHelper.userExists(email)){
Toast.makeText(this, "User Exist", Toast.LENGTH_SHORT).show();
return;
}
}
String timeStamp = "" + System.currentTimeMillis();
boolean id = dbHelper.insertInfo(
"" + imageUri,
"" + email,
"" + name,
"" + age,
"" + phone,
"" + preferenceselected,
"" + password,
""+timeStamp,
""+timeStamp
);
Toast.makeText(this, "Account Created", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, setting.class);
startActivity(intent);
}
您必须检查用户是否存在之后第一个if语句:
if (email.isEmpty() || name.isEmpty() || age.isEmpty() || phone.isEmpty() || preferenceselected.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please fill all the information", Toast.LENGTH_SHORT).show();
return;
}
if (dbHelper.userExists(email)){
Toast.makeText(this, "User Exist", Toast.LENGTH_SHORT).show();
return;
}