I added the method to check the existence of the user's email in the database. For instance, an existing user cannot use the same email to register account again. But I am still able to register my account with the same email. There is no error in my code. I don't know where is the problem. Can you all help me with this problem?
This is the method I add in my 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;
}
This is my 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);
}
You must check if the user exists after the 1st if statement:
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;
}