为什么我的方法在创建新帐户之前无法检查我的用户是否存在?

问题描述 投票:-3回答:1

我添加了一种方法来检查数据库中用户电子邮件的存在。例如,现有用户不能使用同一电子邮件再次注册帐户。但是我仍然可以使用相同的电子邮件注册我的帐户。我的代码没有错误。我不知道问题出在哪里。大家都可以帮我解决这个问题吗?

这是我在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);


}
java android sqlite if-statement android-sqlite
1个回答
0
投票

您必须检查用户是否存在之后第一个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;
   }
© www.soinside.com 2019 - 2024. All rights reserved.