Reputation: 11
I have an old database with the next structure: Database structure
The question is what is the best way to create and populate tables in Room Persistence Database. The way i do it now is a storing tables data in strings.xml and push it into database with firstrun.
public void createDb(final Context context) {
mIsDatabaseCreated.setValue(false);
prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
boolean isFirstRun = prefs.getBoolean(prefKey, true);
Observable.just(Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, AppDatabase.DATABASE_NAME).build())
.map(appDatabase1 -> DatabaseInitUtil.initializeDb(
appDatabase1,
context.getResources().getStringArray(R.array.themes),
isFirstRun))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(appDatabase -> {
mDb = appDatabase;
mIsDatabaseCreated.setValue(true);
});
prefs.edit().putBoolean(prefKey, false).apply();
}
static AppDatabase initializeDb(AppDatabase appDatabase, String[] themes, boolean isFirstRun) {
if (isFirstRun) {
List<ThemeEntity> themesEntities = new ArrayList<>(themes.length);
generateData(themesEntities, themes);
return insertData(appDatabase, themesEntities);
} else {
return appDatabase;
}
}
private static void generateData(List<ThemeEntity> themesEntities, String[] themes) {
for (String theme : themes) {
ThemeEntity themeEntity = new ThemeEntity();
themeEntity.setTheme(theme);
themesEntities.add(themeEntity);
}
}
<string-array name="themes">
<item>@string/courage</item>
<item>@string/death</item>
<item>@string/forgiveness</item>
<item>@string/faith</item>
<item>@string/family</item>
<item>@string/encouragement</item>
<item>@string/friendship</item>
<item>@string/joy</item>
<item>@string/life</item>
<item>@string/love</item>
<item>@string/relationship</item>
<item>@string/strength</item>
</string-array>
But i think that storing a large data in strings arrays and get it with first run it is not right way.
Upvotes: 1
Views: 1068
Reputation: 4838
I known the answer is associated to Room library, but the real problem is that you want to populate a SQLite database when it is created.
What i can do is show how to accomplish this task using another open source library called Kripton Persistence Library.
First of all, as suggested by other answer, it's better to use a JSON file store in the raw folder of your project. Then with Kripton the task is quite simple. In method onCreate of you application class, supposed you database is called QuickStartDataSource, just write code similar to this (it's an incomplete code but i think it understandable):
BindQuickStartDataSource ds = BindQuickStartDataSource.build(DataSourceOptions.builder().databaseLifecycleHandler(new DatabaseLifecycleHandler() {
@Override
public void onCreate(SQLiteDatabase database) {
// read json data from a file
KriptonJsonContext jsonBinder = KriptonBinder.jsonBind();
YouObject bean=jsonBinder.parse(.., YouObject.class);
// insert data
database.execSQL(..);
}
}).build();
Kripton manage persistence on SQLite, SharedPreference and file system too. I suggest you to read its documentation.
For more information about Kripton Persistence Library:
Upvotes: 0
Reputation: 1009
Try making a JOSN of data and store it in assets and when ever you need just push it in the DB and JOSN is much easier to read.
Upvotes: 1