Can we have a multiple databases in single android App using Room? I want to create a new database for every single user. If user1 logged in I have to create a Database for that user like UserDB1, If any new account added I have to create a another Database for that user like UserDb2. If account switch happened between that accounts I should access the user specific Database.How can i achieve that?
I did that like below,
private static AppDatabase buildDatabase(final Context appContext, int userId) {
return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME + "_" + userId)
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
}
})
.build();
}
I understand your requirement totally. There are 3 ways, (Let's say only need a table userTable, but actually we have more.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With