为什么我的方法无法在我的用户能够创建新帐户之前检查我的用户是否存在?
Why my method is not able to check if my user is exist or not before they are able to create a new account?
我添加了检查数据库中是否存在用户电子邮件的方法。例如,现有用户不能使用相同的电子邮件再次注册帐户。但我仍然可以使用相同的电子邮件注册我的帐户。我的代码没有错误。我不知道问题出在哪里。大家能帮我解决这个问题吗?
这是我在 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;
}
我添加了检查数据库中是否存在用户电子邮件的方法。例如,现有用户不能使用相同的电子邮件再次注册帐户。但我仍然可以使用相同的电子邮件注册我的帐户。我的代码没有错误。我不知道问题出在哪里。大家能帮我解决这个问题吗?
这是我在 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;
}