如何在 Room 数据库中使用创建和打开回调

how to use create and open callback in Room database

在代码中,我从 Room.databaseBuilder 中获取了一个实例,添加了一个回调,最后我构建了数据库。 致电

this.mMovieDatabase =  
this.mMovieDBPersistentBuilder.fallbackToDestructiveMigration().build();

我希望调用回调中的日志,但那没有发生。回调中的日志在调用 insertTuplePersistentDB() 时被调用。

我的问题:

1-为什么回调没有被调用

this.mMovieDatabase = 
this.mMovieDBPersistentBuilder.fallbackToDestructiveMigration().build();

这行什么时候执行的??

2-回调中提供的db对象的用途是什么,如何使用它们?它包含类似

的方法
.update()

.delete()

.execSql()

code_1:

 public void buildPersistentDB() {
    Log.v(TAG_LOG, "->buildPersistentDB");
    this.mMovieDBPersistentBuilder = Room
            .databaseBuilder(getApplicationContext(), 
 MovieDatabase.class, ActMain.DATA_BASE_NAME);

    this.mMovieDBPersistentBuilder.addCallback(new 
 RoomDatabase.Callback() {
        @Override
        public void onCreate(@NonNull SupportSQLiteDatabase db) {
            super.onCreate(db);
            Log.w(TAG_LOG + "->onCreate", " buildPersistentDB->DB is 
 created, all tables has been created");
            Log.w(TAG_LOG + "->onCreate", " buildPersistentDB->DB 
 db.getPath(): " + db.getPath());
            Log.w(TAG_LOG + "->onCreate", " buildPersistentDB->DB  
 db.toString(): " + db.toString());
            Log.w(TAG_LOG + "->onCreate", " buildPersistentDB->DB 
 db.isOpen(): " + db.isOpen());
            Log.w(TAG_LOG + "->onCreate", " buildPersistentDB->DB 
 db.isReadOnly(): " + db.isReadOnly());
        }

        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
            Log.w(TAG_LOG + "->onOpen", " 
 buildPersistentDB->onCreate\n");
            Log.w(TAG_LOG + "->onOpen", " buildPersistentDB->DB has been 
 opened");
        }
    });
    this.mMovieDatabase = 
 this.mMovieDBPersistentBuilder.fallbackToDestructiveMigration().build();
 }

 private void insertTuplePersistentDB(int recordNum) {
    Log.v(TAG_LOG, "->insertTuplePersistentDB is called. recodNum: " + 
 recordNum);

    Movie movie = new Movie();
    for (int i = 1; i <= recordNum; i++) {
        movie.setMovieId(String.valueOf(i));
        movie.setMovieName("Movie_episode_" + String.valueOf(i));
        mMovieDatabase.dao().insertOnlySingleMovie(movie);
    }
 }

1-why the callbacks has not been called

onCreate 回调只会在数据库的生命周期内调用一次,即在打开文件时调用。

  • 它允许您做其他事情,例如或许创建一个 table 或 tables 是或不被 Room 覆盖。

只要打开数据库,就会调用 onOpen 回调(见下文)

  • 它可以让你做一些 Room 没有涵盖的事情。

只是建立数据库不会打开底层的SQLite数据库,直到您尝试访问数据库,即获取、添加、删除或更新数据时,数据库才会打开必要时打开和创建。

2-what is the purpose of the db object provided in the callback, and how to use them??it contains methods like ....

db 是一个 SupportSQLiteDatabase 对象,如果使用回调,您可能想要做一些 Room 不支持的事情(例如添加这个 table 出于某种原因未被房间覆盖)。要添加 table,您需要能够 运行 SQL 来创建 table,然后您将使用 db.execSQL(your_SQL_as_a_String);,因此支持SQLiteDatabase 已通过。

在 Room 的术语支持中SQLiteDatabase 是:-

  • A database abstraction which removes the framework dependency and allows swapping underlying sql versions. It mimics the behavior of SQLiteDatabase.