objectbox-java
objectbox-java copied to clipboard
Data export and import
Allow an data (and optionally the data model) to be exported and imported. This would simplify packaging an initial dataset with an app.
Possible implementations: json.gz, binary.gz, raw dump.
Update: you can prepare a data.mdb file to have preloaded data in your app; see FAQ.
Wow~ Could you please share some time schedule for this feature? @greenrobot
Extremely useful feature! Looking forward!
This would greatly simplify some work I'm doing -- I'd love to just download a fully built MDB file to the app, or some intermediary format that can be "automatically" parsed to replace the on-device database.
It's weird but this "feature" already available. When I looked for how to do it I found this issue on github and thought that it's impossible to pack initial dataset into my app. But then I noticed question in FAQ section of official site:
Can I ship my app with a pre-built database? Yes. ObjectBox stores all data in a single database file. Thus, you just need to prepare this DB file and copy it to the correct location on the first start of your app (before you touch ObjectBox’s API). The DB file is called “data.mdb” and is typically located in a subdirectory called “objectbox” (or any name you passed to BoxStoreBuilder). On Android, the DB file is located inside the app’s files directory inside “objectbox/objectbox/” or, for example “objectbox/yourname” if you assigned the custom name “yourname” using BoxStoreBuilder.
As for me it's enough. Maybe it would be useful for thoose who looking for solution.
@Yazon2006 where are you find the FAQ? would you please share the url?
@yocn in very unexpected place: http://objectbox.io/faq/
@greenrobot So, Is the Can I ship my app with a pre-built database? in FAQ recommend for developers?
@yocn Yes? Not sure what you are getting at with recommended. -ut
So we just double checked: the data.mdb file works across plaforms (x64, ARM, 32 and 64 bit). Thus, the FAQ is still valid.
@greenrobot So,If I want to develop with a preset data.mdb, my process is: 1、I generate a data.mdb with my preset datas. 2、get a default path in data/data/myPackageName/file/objectbox/data.mdb 3、replace the default data.mdb with my data.mdb. Is this correct? And does objectbox have a API to get default data.mdb location?
Yes, this is mostly correct. Just a few comments:
- It would be better if you would create the file before opening ObjectBox (data.mdb should be non-existent)
- better way to get to the base path is via
context.getFilesDir()
(don't do "data/data/myPackageName/file") - default path from the files dir is "/objectbox/objectbox" (the latter "objectbox" is the adjustable db name)
- There's a non-public method
BoxStoreBuilder.getAndroidDbDir()
, you can take as reference
I think it makes sense to make a convenient method to provision a default db file, that copies some file if the actual db file does not exist. What do you think?
I'm just implementing a method like this:
BoxStoreBuilder initialDbFile(Factory<InputStream> initialDbFileFactory)
Because in Android assets and raw resources are based on InputStream, this should be convenient.
Check 1.4.4 for BoxStoreBuilder.initialDbFile(...)
which will provide a initial data file if it does not exist before.
@greenrobot Really thanks, I'll check it later.
@greenrobot
Sorry to disturb,I can't find API on http://objectbox.io/files/objectbox-java/current/
I use BoxStoreBuilder.initialDbFile(...)
method like below:
BoxStoreBuilder builder = MyObjectBox.builder();
Factory<InputStream> is = new Factory<InputStream>() {
@Override
public InputStream provide() throws Exception {
LogUtil.d("yocn Factory<InputStream> provide");
InputStream is = context.getResources().getAssets().open("data.mdb");
return is;
}
};
boxStore = builder.initialDbFile(is).androidContext(context).build();
Is My invoke method correct?
Looks good! Online JavaDocs are still to be updated...
@greenrobot but I cannot get the provide()
callback in Factory<InputStream>
, LogUtil.d("yocn Factory<InputStream> provide");
cannot be printed, I don't know where I did wrong or it is normal behavior
@yocn Will only be called if there's no DB yet.
@greenrobot Yes~~~forget it , first run is normal, appreciate it.
@greenrobot But I have another question,Whether the mdb file is cross-platform,For example, I am generating on x86, can I use it on armbi-v7a ?
I think I found the answer. It is cross-platform,
https://github.com/objectbox/objectbox-java/issues/310#issuecomment-367098696
Sorry to disturb
Do I get this right, you are creating your databases on runtime only, so each onCreate
you have to create your database? Or do you save your database somewhere internally once programmatically but you cannot just edit it outside in something like SQLite Database Browser
and append it?
Do I get this right, that it's still this way today?
If so, how does this get the "appreciation" it seems to get with 2830 stars?
@ErfolgreichCharismatisch ObjectBox persists data across app starts of course.
Updated link in first comment to point to new FAQ page: https://docs.objectbox.io/faq#can-i-ship-my-app-with-a-pre-built-database -ut
What is the best way to generate an initial data.mdb and keep it readable?
With SQLite, we just need to write some sql scripts and commit to VCS. It is just text file, so we can understand the initial data.
I suggest add a feature to import json file (ex. data.json
) as initial data. We can allow to configure gradle plugin io.objectbox
, so it can convert the provided data.json
to data.mdb
. Then in the app, we can use this data.mdb
as preloaded data.
Hi, don't know if this was mentioned anywhere else but is there any way of reading the .mdb file directly?
I'd like to allow the user to be able to import a database, either this deletes the current one and puts the imported one into the file system(should be easy enough) or (even better) the entries in the imported database are added to those in the current database.
I don't know if this is already implemented or if you have a recommended way of tackling this, I thought of reading all the entries and putting them in a .json file and then that is what's exported. And when we import we just read the .json file and persist entries from the .json file, using GSON.
Any help would be appreciated, many thanks and thank you for making my life easier with Objectbox!
I keep getting this io.objectbox.exception.DbException: Incoming entity ID 1:XXX does not match existing UID XYZ
exception on app startup when I try to load a pre-built data.mdb, although there hasn't been a previous database on the device because I got this exception from the beginning.
I uninstall the app every time, do a gradle clean, delete the build folder, do a fresh build and still this error persists. Also if I delete the default.json. If I skip loading the pre-built DB and start with an empty one, there is no problem.
This is really frustrating as I don't know what I am doing wrong.
[edit] Got it to work by copying the default.json from my Java project to the android project, so that the IDs match. Don't know what caused the confusion though.
@Benjoyo This sounds like you found out that your model file changed unexpectedly. Make sure to also commit it to version control. https://docs.objectbox.io/getting-started#model-file
-Uwe