Регистрация, ошибка при нажатии
Пытаюсь сделать регистрацию пользователей, делал по этому примеру https://www.youtube.com/watch?v=1WPAXHhG6u0, но при нажатии зарегистрироваться вылетает ошибка, я проверил, вроде бы всё правильно сделал, но так и не понял почему ошибка вылетает, подскажите как исправить.
ActivityProfileRegistration.java
public class ActivityProfileRegistration extends AppCompatActivity {
DBHelper db;
EditText surname, name, patronymic, numberphone, email, password, repassword;
Button registration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_registration);
surname = findViewById(R.id.surname);
name = findViewById(R.id.name);
patronymic = findViewById(R.id.patronymic);
numberphone = findViewById(R.id.numberphone);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
registration = findViewById(R.id.registration);
repassword = findViewById(R.id.repassword);
db = new DBHelper(this);
registration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s1 = email.getText().toString();
String s2 = password.getText().toString();
String s3 = repassword.getText().toString();
if (s1.equals("") || s2.equals("") || s3.equals("")) {
Toast.makeText(getApplicationContext(), "Fields are empty", Toast.LENGTH_SHORT).show();
} else {
if (s2.equals(s3)) {
Boolean chkemail = db.chkemail(s1);
if (chkemail == true) {
Boolean insert = db.insert(s1, s2);
if (insert == true) {
Toast.makeText(getApplicationContext(), "Registerd Successfully", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Email Already exists", Toast.LENGTH_SHORT).show();
}
}
Toast.makeText(getApplicationContext(), "Password do not match", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_profile_registration.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="144dp"
android:layout_height="144dp"
android:src="@drawable/ic_baseline_account_circle_24" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Фамилия"
android:inputType="textCapSentences"
android:paddingHorizontal="6dp"
android:textColor="@color/color_2"
android:textColorHint="@color/color_3"
android:textCursorDrawable="@drawable/cursor"
android:textSize="24sp" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Имя"
android:inputType="textCapSentences"
android:paddingHorizontal="6dp"
android:textColor="@color/color_2"
android:textColorHint="@color/color_3"
android:textCursorDrawable="@drawable/cursor"
android:textSize="24sp" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/patronymic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Отчество"
android:inputType="textCapSentences"
android:paddingHorizontal="6dp"
android:textColor="@color/color_2"
android:textColorHint="@color/color_3"
android:textCursorDrawable="@drawable/cursor"
android:textSize="24sp" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/numberphone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Номер телефона"
android:inputType="phone"
android:paddingHorizontal="6dp"
android:textColor="@color/color_2"
android:textColorHint="@color/color_3"
android:textCursorDrawable="@drawable/cursor"
android:textSize="24sp" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Электронная почта"
android:inputType="textEmailAddress"
android:paddingHorizontal="6dp"
android:textColor="@color/color_2"
android:textColorHint="@color/color_3"
android:textCursorDrawable="@drawable/cursor"
android:textSize="24sp" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Пароль"
android:inputType="textPassword"
android:paddingHorizontal="6dp"
android:textColor="@color/color_2"
android:textColorHint="@color/color_3"
android:textCursorDrawable="@drawable/cursor"
android:textSize="24sp" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/repassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Повторите пароль"
android:inputType="textPassword"
android:paddingHorizontal="6dp"
android:textColor="@color/color_2"
android:textColorHint="@color/color_3"
android:textCursorDrawable="@drawable/cursor"
android:textSize="24sp" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:backgroundTint="@color/color_2"
app:cardCornerRadius="6dp"
app:cardElevation="0dp">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<Button
android:id="@+id/registration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_6"
android:drawableEnd="@drawable/ic_baseline_check_24"
android:drawableRight="@drawable/ic_baseline_check_24"
android:drawablePadding="6dp"
android:paddingHorizontal="6dp"
android:text="Зарегистрироваться"
android:textAllCaps="false"
android:textColor="@color/color_2"
android:textSize="24sp"
android:textStyle="normal" />
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
super(context, "Login.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table user(email text primary key, password text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists user");
}
public boolean insert(String email, String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email",email);
contentValues.put("password",password);
long ins=db.insert("user",null,contentValues);
if(ins==-1)return false;
else return true;
}
public Boolean chkemail(String email){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from user where email=?",new String[]{email});
if(cursor.getCount()>0) return false;
else return true;
}
public Boolean emailpassword(String email,String password){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from user where email=? and password=?", new String[]{email,password});
if(cursor.getCount()>0) return true;
else return false;
}
}
Ошибка которая появляется при нажатии на кнопку
android.database.sqlite.SQLiteException: no such table: user (code 1): , while compiling: Select * from user where email=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:897)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:508)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
at com.example.myapplication.DBHelper.chkemail(DBHelper.java:37)
at com.example.myapplication.ActivityProfileRegistration$1.onClick(ActivityProfileRegistration.java:43)
at android.view.View.performClick(View.java:5692)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6397)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:829)
Ругается на
at com.example.myapplication.DBHelper.chkemail(DBHelper.java:37)
at com.example.myapplication.ActivityProfileRegistration$1.onClick(ActivityProfileRegistration.java:43)