-1

I know this a duplicate question but I have searched all of the same questions and nothing solved my problem. I am trying to create a sqlite database that contains one table(users_information) when the user presses the register button I check if his email exists in that table or not but the database doesn't create(I have looked in the project data files and found nothing) besides there is no obvious exception appears?

Here is myDatabaseContract class:

package com.example.registerandlogingapp;

import android.provider.BaseColumns;

public class MyDatabaseContract {

class UsersInformation implements BaseColumns {

    public static final String TABLE_NAME = "Users_Information";

    public static  final String USER_EMAIL_COLUMN = "User_Email";

    public static final String USER_PASSWORD_COLUMN = "User_Password";


}

} 

And here is myDatabaseHelper class :

package com.example.registerandlogingapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "MyDatabase.db";

private static final int DATABASE_VERSION = 1;

private static  String sqlCreateTable ;

public MyDatabaseHelper( Context context) {

super(context, DATABASE_NAME,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

sqlCreateTable = "CREATE TABLE  " + MyDatabaseContract.UsersInformation.TABLE_NAME

+ "(" + MyDatabaseContract.UsersInformation._ID +" INTEGER PRIMARY KEY," + MyDatabaseContract.UsersInformation.USER_EMAIL_COLUMN +" TEXT,"

+ MyDatabaseContract.UsersInformation.USER_PASSWORD_COLUMN + " TEXT);"; 

 db.execSQL(sqlCreateTable;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

 }
 }

And here is the RegisterActivity :

   package com.example.registerandlogingapp;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
 import  android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {

MyDatabaseHelper myDatabaseHelper;

SQLiteDatabase myDatabase;

Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);


setContentView(R.layout.activity_register);

myDatabaseHelper = new MyDatabaseHelper(this);

myDatabase = myDatabaseHelper.getReadableDatabase();

Button registerButton = (Button) findViewById(R.id.registerButton);


registerButton.setOnClickListener(registerButtonListener);
}
 View.OnClickListener registerButtonListener = new View.OnClickListener()  {
  @Override
  public void onClick(View v) {

     String [] projection = { MyDatabaseContract.UsersInformation.USER_EMAIL_COLUMN };

     cursor = myDatabase.query(MyDatabaseContract.UsersInformation.TABLE_NAME,projection,null,null,null,null,

    
MyDatabaseContract.UsersInformation.USER_EMAIL_COLUMN);

     EditText emailEditText= (EditText) findViewById(R.id.emailEditText);

     String email = emailEditText.getText().toString();

     
while(cursor.moveToNext())
     {
         
Log.d("User_Email",cursor.getString(0));

         
if(cursor.getString(0).equals(email))
         {
             
Toast.makeText( getApplicationContext(),"This email already exists, please enter a different email",Toast.LENGTH_SHORT).show();
         }
         else {

             
Toast.makeText( getApplicationContext(),"You have successfully registered",Toast.LENGTH_SHORT).show();
         }
     }
     if(cursor != null)
     {
         
cursor.close();
     }
 }
 };

 @Override
 protected void onDestroy() {

if(myDatabaseHelper != null) {

    myDatabaseHelper.close();
}

if(myDatabase != null)
{
    myDatabase.close();
}

super.onDestroy();
}
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

1 Answers1

0

Try this [MyDatabaseHelper edited]

public static class MyDatabaseHelper extends SQLiteOpenHelper
{
    
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "MyDatabase.db";
    private static  String sqlCreateTable ;

    public MyDatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

     @Override
     public void onCreate(SQLiteDatabase db) {
            sqlCreateTable = "CREATE TABLE IF NOT EXISTS " + MyDatabaseContract.UsersInformation.TABLE_NAME + "("
                 + MyDatabaseContract.UsersInformation._ID + " INTEGER PRIMARY KEY ," + MyDatabaseContract.UsersInformation.USER_EMAIL_COLUMN + " TEXT NOT NULL,"
                 + MyDatabaseContract.UsersInformation.USER_PASSWORD_COLUMN + " TEXT )" ;

            db.execSQL(sqlCreateTable);             

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + MyDatabaseContract.UsersInformation.TABLE_NAME);
           
        }}
MdBasha
  • 423
  • 4
  • 16