0

I've created a Database helper class for SQLite database and I want to create an instance of it inside my fragment. But no table is created.

UserDatabaseHelper.java

public static final String DATABASE_NAME = "Users.db" ;
public static final String TABLE_NAME = "User_table" ;
public static final String COL_1 = "Username" ;
public static final String COL_2 = "Email" ;
public static final String COL_3 = "Password" ;

public UserDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table "+ TABLE_NAME+ "(Username TEXT PRIMARY KEY ," +
            " Email TEXT, Password TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);

}



public boolean insertData(String username , String email , String password ) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues() ;
    contentValues.put(COL_1,username);
    contentValues.put(COL_2,email);
    contentValues.put(COL_3,password);
    long result = db.insert(TABLE_NAME , null , contentValues) ;
    if (result == -1) return false ;
    else return true ;
}

public boolean checkUser(String username) {

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from user where username=?",new String[]{username}) ;
    if (cursor.getCount()>0) return false ;
    else return true ;
}

SignUpFragment.java

UserDatabaseHelper myDb ;
EditText username_signup , email_signup , password_signup , password_signup_repeat ;
CheckBox checkup ;
Button signup_btn ;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.signup_fragment,container ,false);

    username_signup = view.findViewById(R.id.username_signup);
    email_signup = view.findViewById(R.id.email_signup);
    password_signup = view.findViewById(R.id.password_signup);
    password_signup_repeat = view.findViewById(R.id.password_signup_repeat);
    checkup = view.findViewById(R.id.checkup);
    signup_btn = view.findViewById(R.id.signup_btn);
    myDb = new UserDatabaseHelper(getActivity());



    return view;
}

After creating an instance the database table is supposed to be created but nothing happens. I've tried to change my code to

SQLiteDatabase myDb ;

and then

myDb = new UserDatabaseHelper(getActivity()).getWritableDatabase(); 

In this condition the table is created but I can't call the checkuser and insert methods which are in the helper class. What should I do? What's wrong with my code?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Unicorn
  • 37
  • 8
  • your `getActivity()` might be still `null` during `onCreateView`. Try moving `myDb = new UserDatabaseHelper(getActivity());` into `onActivityCreated` – Vladyslav Matviienko Dec 10 '18 at 09:15
  • pls call method , myDb = new UserDatabaseHelper(getActivity()); myDb.insertData("name","email","password"); – Praveen Dec 10 '18 at 09:34
  • Thank you for your response. I tried these solutions but still no table is created. – Unicorn Dec 10 '18 at 09:40

1 Answers1

1

After creating an instance the database table is supposed to be created but nothing happens.

Database is only created when you call one of the getWritableDatabase() or getReadableDatabase() methods. Creating an SQLiteOpenHelper instance is not enough.

See also: When is SQLiteOpenHelper onCreate() / onUpgrade() run?

myDb = new UserDatabaseHelper(getActivity()).getWritableDatabase(); 

In this condition the table is created but I can't call the checkuser and insert methods which are in the helper class.

You don't need to do this. Just store the helper reference and call your checkuser() and insert() methods on it. These methods call get...Database() to create the database if needed.

laalto
  • 150,114
  • 66
  • 286
  • 303