Checking if emailis in database, otherwise store username and password in database
我正在尝试通过一个方法 (register();) 检查注册页面中的字段是否为空。在我想检查电子邮件是否存储在 SQLite 数据库中之后,如果没有,请将电子邮件和密码存储在数据库中。以下是我的代码:
数据库(用户方法)
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public boolean insertUser(UserModel userModel) { String password; password = getSecurePassword(userModel.getPassword(), "Easy Pill"); SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("FIRSTNAME", userModel.getFirstName()); values.put("LASTNAME", userModel.getLastName()); values.put("EMAIL", userModel.getEmail()); values.put("AGE", userModel.getAge()); values.put("PASSWORD", password); long result = db.insert(TABLE_USER, null, values ); if(result == –1) return false; else return true; } public boolean insertUserData(String email, String password){ public Boolean getLoginInfo(UserModel user){ |
注册活动
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
public class RegisterActivityController extends AppCompatActivity { private EditText firstName, lastName, dateOfBirth, email, password, confirmPassword; private String first, last, birth, emailAdd, passwd, conPasswd; Calendar myCalendar = Calendar.getInstance(); @Override final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { }; dateOfBirth.setOnClickListener(new View.OnClickListener() { @Override private void updateLabel() { public void register() { public void initialize() { public boolean validate() { public void onSignupSuccess() { |
Register.xml
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.constraint.ConstraintLayout <EditText <EditText <EditText <EditText <EditText <EditText <Button </android.support.constraint.ConstraintLayout> |
我对此进行了大量研究并尝试了一些不同的方法,但我需要在注册活动中运行注册方法,如果一切正常,那么我需要检查电子邮件和密码然后将它们都存储起来。任何和所有的帮助将不胜感激。
首先,您可以将以下方法添加到您的数据库(用户方法)类中:-
1
2 3 4 5 6 7 8 9 |
然后你可以在 register 方法的适当位置包含一个检查,沿着
的行
1
2 3 4 5 |
if (!db.isEmailUnique(emailAdd))
{ email.setError("Please enter a Unique Email Address."); valid = false; } |
或者你可以改变:-
1
2 3 4 |
if (email.length() == 0 || !Patterns.EMAIL_ADDRESS.matcher(emailAdd).matches()) {
email.setError("Please enter a valid Email Address."); valid = false; } |
成为:-
1
2 3 4 |
if (email.length() == 0 || !Patterns.EMAIL_ADDRESS.matcher(emailAdd).matches() || !db.isEmailUnique(emailAdd)) {
email.setError("Please enter a valid Email Address."); valid = false; } |
额外的
您似乎也没有调用
我建议对寄存器 mnethod 进行以下更改:-
1
2 3 4 5 6 7 8 9 10 11 12 |
public void register() {
initialize(); if (!validate()) { Toast.makeText(this,"Failed to create account.", Toast.LENGTH_SHORT).show(); } else { If (db.insertUser(emailAdd,passwd)) { onSignupSuccess(); } else { Toast.makeText(this,"Failed to create account (insert into database did not insert a row.).", Toast.LENGTH_SHORT).show(); } } } |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/267564.html