gpt4 book ai didi

java - 检查电子邮件是否在数据库中,否则将用户名和密码存储在数据库中

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:36 27 4
gpt4 key购买 nike

我正在尝试运行一个方法(register();),该方法检查注册页面中的字段是否为空。我想检查电子邮件是否存储在 SQLite 数据库中,如果没有,则将电子邮件和密码存储在数据库中。以下是我的代码:

数据库(用户方法)

 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){
ContentValues contentValues = new ContentValues();
contentValues.put("EMAIL", email);
contentValues.put("PASSWORD", password);
long result = db.insert("USER", null, contentValues );
if(result == -1)
return false;
else
return true;
}

public Boolean getLoginInfo(UserModel user){
SQLiteDatabase db = this.getReadableDatabase();
String password;
password= getSecurePassword(user.getPassword(), "Easy Pill");
String query = "Select EMAIL, PASSWORD FROM " + TABLE_USER + " WHERE EMAIL = '"+user.getEmail() +"' AND PASSWORD= '"+password+"'";
Cursor resultSet = db.rawQuery(query, null);
if(resultSet.getCount()== 0)
return false;
else
return true;
//resultSet.close();
}
}

注册 Activity

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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
firstName = findViewById(R.id.firstName);
lastName = findViewById(R.id.lastName);
dateOfBirth = findViewById(R.id.dateOfBirth);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
confirmPassword = findViewById(R.id.confirmPassword);
Button createButton = findViewById(R.id.createButton);
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register();
}
});

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}

};

dateOfBirth.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(RegisterActivityController.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}

private void updateLabel() {
String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dateOfBirth.setText(sdf.format(myCalendar.getTime()));
}

public void register() {
initialize();
if (!validate()) {
Toast.makeText(this, "Failed to create account.", Toast.LENGTH_SHORT).show();
} else {
onSignupSuccess();
}
}

public void initialize() {
first = firstName.getText().toString().trim();
last = lastName.getText().toString().trim();
birth = dateOfBirth.getText().toString().trim();
emailAdd = email.getText().toString().trim();
passwd = password.getText().toString().trim();
conPasswd = confirmPassword.getText().toString().trim();
}

public boolean validate() {
boolean valid = true;
if (firstName.length() == 0 || firstName.length() > 32) {
firstName.setError("Please enter a valid first name.");
valid = false;
}
if (lastName.length() == 0 || lastName.length() > 32) {
lastName.setError("Please enter a valid last name.");
valid = false;
}
if (dateOfBirth.length() == 0 || TextUtils.isEmpty(dateOfBirth.getText().toString())) {
dateOfBirth.setError("Please enter your date of birth.");
valid = false;
}
if (email.length() == 0 || !Patterns.EMAIL_ADDRESS.matcher(emailAdd).matches()) {
email.setError("Please enter a valid Email Address.");
valid = false;
}
if (password.length() == 0) {
password.setError("Please enter password.");
valid = false;
}
if (confirmPassword.length() == 0 || !passwd.equals(conPasswd)) {
confirmPassword.setError("Please reenter password or make sure passwords match.");
valid = false;
}
return valid;
}

public void onSignupSuccess() {
Toast.makeText(getBaseContext(), "User account: " + first + " " + last + ", created.", Toast.LENGTH_SHORT).show();
}
}

注册.xml

<?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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".login.activity.controllers.RegisterActivityController">


<EditText
android:id="@+id/firstName"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="@string/first_name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/lastName"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/last_name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/firstName" />

<EditText
android:id="@+id/dateOfBirth"
android:clickable="true"
android:focusable="false"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/date_of_birth"
android:inputType="date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lastName" />

<EditText
android:id="@+id/email"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/add_email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dateOfBirth" />

<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/add_password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email" />

<EditText
android:id="@+id/confirmPassword"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/confirm_password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />

<Button
android:id="@+id/createButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="148dp"
android:layout_marginStart="148dp"
android:layout_marginTop="24dp"
android:text="@string/create_account"
android:onClick="touchRegisterUser"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/confirmPassword" />

</android.support.constraint.ConstraintLayout>
</ScrollView>

我对此进行了大量研究并尝试了一些不同的方法,但我需要在注册 Activity 中运行注册方法,如果一切正常,那么我需要检查电子邮件和密码,然后存储它们。任何和所有的帮助将不胜感激。

最佳答案

首先,您可以将以下方法添加到数据库(用户方法)类中:-

public boolean isEmailUnique(String email) {
int count=0;
String whereclause = "EMAIL=?";
String[] whereargs = new String[]{email};
Cursor csr =db.query("USER",null,whereclause,whereargs,null,null,null);
count = csr.getCount();
csr.close();
return count < 1;
}

然后您可以在注册方法中的适当位置包含一个检查,类似于

if (!db.isEmailUnique(emailAdd)) 
{
email.setError("Please enter a Unique Email Address.");
valid = false;
}

或者你可以改变:-

if (email.length() == 0 || !Patterns.EMAIL_ADDRESS.matcher(emailAdd).matches()) {
email.setError("Please enter a valid Email Address.");
valid = false;
}

成为:-

if (email.length() == 0 || !Patterns.EMAIL_ADDRESS.matcher(emailAdd).matches() || !db.isEmailUnique(emailAdd)) {
email.setError("Please enter a valid Email Address.");
valid = false;
}

附加

您似乎也没有调用 insertUser 方法来实际添加用户。

我建议对寄存器方法进行以下更改:-

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();
}
}
}

关于java - 检查电子邮件是否在数据库中,否则将用户名和密码存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49833221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com