2016-01-16 56 views
0

這段代碼應該是有意爲之,以驗證已添加到「配置文件」數據庫的電子郵件地址之前沒有輸入過。用戶輸入相關數據並嘗試將數據驗證爲合法條目後,會根據數據庫中的其他條目檢查電子郵件地址(因爲電子郵件通常是暱稱以外的一組條目中最獨特的部分)和如果電子郵件對於數據庫是唯一的,則該條目將被接受,並且應該在數據庫中創建一個新列(它是)。問題在於電子郵件始終被認爲是唯一的。我在android studio的代碼中遇到了問題

這段代碼顯示的條目是如何淘汰,以確保它們符合報名表

public void registerAccount(View view) { 
    LoginEntries entries = new LoginEntries(
      newEmailAddressInput.getText().toString(), 
      newPasswordInput.getText().toString(), 
      newFirstNameInput.getText().toString(), 
      newLastNameInput.getText().toString(), 
      newNickname.getText().toString(),fullPhoneNumber); 
    String isTempEmail = newEmailAddressInput.getText().toString(); 

    String isTempPass = newPasswordInput.getText().toString(); 
    String confirmPasswordHolder = confirmNewPasswordInput.getText().toString(); 

    if (TextUtils.isEmpty(isTempEmail) && TextUtils.isEmpty(isTempPass)) { 
     Toast.makeText(this, "Enter Email and Password", Toast.LENGTH_LONG).show(); 
     newEmailAddressInput.setText(""); 
     newPasswordInput.setText(""); 
     confirmNewPasswordInput.setText(""); 
    } else if (TextUtils.isEmpty(isTempEmail) && !TextUtils.isEmpty(isTempPass)) { 
     Toast.makeText(this, "Enter Email", Toast.LENGTH_LONG).show(); 
     newEmailAddressInput.setText(""); 
     newPasswordInput.setText(""); 
     confirmNewPasswordInput.setText(""); 
    } else if (!TextUtils.isEmpty(isTempEmail) && TextUtils.isEmpty(isTempPass)) { 
     Toast.makeText(this, "Enter Password", Toast.LENGTH_LONG).show(); 
     newEmailAddressInput.setText(""); 
     newPasswordInput.setText(""); 
     confirmNewPasswordInput.setText(""); 
    } /*Temporary while app is offline, when app will be operational, different prompt will search web to verify email address*/ 
    else if (!isTempEmail.endsWith("@gmail.com") && !isTempEmail.endsWith("@yahoo.com") && !isTempEmail.endsWith("@aol.com") && !isTempEmail.endsWith("@hotmail.com")) { 
     Toast.makeText(this, "Not a valid email address, trying again", Toast.LENGTH_LONG).show(); 
     newEmailAddressInput.setText(""); 
     newPasswordInput.setText(""); 
     confirmNewPasswordInput.setText(""); 
    } else if (!dbHandler.signUpEmailCheck(isTempEmail)) { 
     Toast.makeText(this, "Email used, please try again", Toast.LENGTH_LONG).show(); 
     newEmailAddressInput.setText(""); 
     newPasswordInput.setText(""); 
     confirmNewPasswordInput.setText(""); 
    } else if (!confirmPasswordHolder.equals(isTempPass)) { 
     Toast.makeText(this, "Passwords don't match!", Toast.LENGTH_LONG).show(); 
     newPasswordInput.setText(""); 
     confirmNewPasswordInput.setText(""); 
     areaCodeInput.setText(""); 
     firstThreeDigitsInput.setText(""); 
     finalFourDigitsInput.setText(""); 
    } else{ 
     Toast.makeText(this, "Saved!", Toast.LENGTH_LONG).show(); 
     dbHandler.addEntry(entries);// adds entries to database 
     printDatabase(); 
     } 
} 

的規定,這個代碼,這是上面的方法就在眼前的問題。當電子郵件移交給此代碼時,它會從數據庫處理程序類中調用以下方法。

else if (!dbHandler.signUpEmailCheck(isTempEmail)) { 
      Toast.makeText(this, "Email used, please try again", Toast.LENGTH_LONG).show(); 
      newEmailAddressInput.setText(""); 
      newPasswordInput.setText(""); 
      confirmNewPasswordInput.setText(""); 

該方法以電子郵件條目,並使用它來調用任何及所有其他條目名稱相同(據我瞭解它的工作,我可能是錯這就是爲什麼我寫了這個問題) 如果getColumnCount()函數不爲零,該方法返回true,這表明if語句爲false,它指示用戶使用另一封電子郵件。這絕不會發生。我已經嘗試了不同的真實,虛假回報,但沒有一個組合導致正確的結論。

public boolean signUpEmailCheck(String emailEntry){ 
    //checks if a new email entry already exists in the database 

    Integer holder; 
    SQLiteDatabase db = getWritableDatabase(); 
    String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE " + COLUMN_EMAILADDRESS + "=\"" + emailEntry + "\""; 
    Cursor c = db.rawQuery(query,null); 
    holder = c.getColumnCount(); 
    if (holder > 0) { 
     db.close(); 
     c.close(); 
     return true; 
    } else { 
     db.close(); 
     c.close(); 
     return false; 
    } 
} 

這是被稱爲將數據輸入到數據庫中

public void addEntry(LoginEntries entry){ 
    ContentValues values = new ContentValues(); 

    values.put(COLUMN_EMAILADDRESS,entry.get_emailAddress()); 
    values.put(COLUMN_PASSWORD,entry.get_password()); 
    values.put(COLUMN_FIRSTNAME,entry.get_firstName()); 
    values.put(COLUMN_LASTNAME,entry.get_lastName()); 
    values.put(COLUMN_PHONENUMBER,entry.get_phoneNumber()); 
    values.put(COLUMN_NICKNAME,entry.get_nickname()); 
    SQLiteDatabase db = getWritableDatabase(); 
    db.insert(TABLE_LOGINENTRIES, null, values); 
    db.close(); 
} 

回答

1

嘗試重寫查詢這樣的方法,也請務必emailEntrynull

String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE " + COLUMN_EMAILADDRESS + "=?"; 
Cursor c = db.rawQuery(query, new String[] { emailEntry }); 

也只是檢查光標是空的,下面的方法:

if(!c.moveToFirst() || c.getCount() == 0){ 
    ... 
    return true; // cursor is empty, the email doesn't exist 
}else{ 
    return false; // cursor is not empty, the email exists 
} 
+0

謝謝。我終於可以繼續討論其他50個bug了。 一個問題,!c.moveToFirst()怎麼玩? –

+0

如果光標爲空,'moveToFirst()'將返回false。如果它幫助你解決問題,請接受我的回答。 – cylon

+0

謝謝你的幫助。我已經做了。 –