SQLiteOpenHelper “onCreate” is not called? (the DB does not exist)

前端 未结 5 1140
[愿得一人]
[愿得一人] 2021-01-19 21:54

From a fragment I instantiate this way

fmdata = new FileManagerData(getActivity());

the following class. I don\'t understand why onCreate()

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-19 22:45

    I know that this thread has already accepted answer. Neverthless this is what i have to add:

    i have:

        protected class OpenHelper extends SQLiteOpenHelper {
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                 MHSqlDb.this.createTables(db);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    I'm calling this:

        iOpenHelper = new OpenHelper(iDbFileName, DATABASE_VERSION );
    

    The databese file doesn't exist at this stage.

    Then i do some debugging of the app. i never call

        iOpenHelper.getReadableDatabase();
    

    neither i call

        iOpenHelper.getWritableDatabase();
    

    the database file still does not exist. Now when i kill the app from debugger i can suddenly see database file (with the name in iDbFileName) created. The onCreate method of the OpenHelper is never called.

    So while i agree that the intention of onCreate method seems to be to get called when the database file doesn't exist and either getReadableDatabase() or getWritableDatabase() is called, this is apparently not always the case.

提交回复
热议问题