How to select from multiple databases in Android Room(How to attach databases)

后端 未结 2 450
旧巷少年郎
旧巷少年郎 2020-12-16 01:04

As you know we can select from multiple databases using attach command like this:

String path = DBHelper.getDatabasePath(context);
String sql = \"ATTACH DATA         


        
2条回答
  •  执念已碎
    2020-12-16 01:35

    While building room database using the DatabaseBuilder class, you have an option to register a callback which has methods that'd be called everytime your database is opened:

     /**
             * Called when the database has been opened.
             *
             * @param db The database.
             */
            public void onOpen(@NonNull SupportSQLiteDatabase db) {
            }
    

    One option is to register this callback and ATTACH your other database using the db parameter. Then in dao, you can have queries which reference the other table.

提交回复
热议问题