Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data export and import #310

Open
greenrobot opened this issue Dec 12, 2017 · 27 comments
Open

Data export and import #310

greenrobot opened this issue Dec 12, 2017 · 27 comments
Labels
enhancement New feature or request

Comments

@greenrobot
Copy link
Member

greenrobot commented Dec 12, 2017

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.

@yocn
Copy link

yocn commented Dec 13, 2017

Wow~ Could you please share some time schedule for this feature? @greenrobot

@Yazon2006
Copy link

Extremely useful feature! Looking forward!

@bigntallmike
Copy link

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.

@Yazon2006
Copy link

Yazon2006 commented Jan 26, 2018

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.

@yocn
Copy link

yocn commented Jan 31, 2018

@Yazon2006 where are you find the FAQ? would you please share the url?

@Yazon2006
Copy link

@yocn in very unexpected place: http://objectbox.io/faq/

@yocn
Copy link

yocn commented Feb 6, 2018

@greenrobot So, Is the Can I ship my app with a pre-built database? in FAQ recommend for developers?

@greenrobot-team
Copy link
Member

@yocn Yes? Not sure what you are getting at with recommended. -ut

@greenrobot
Copy link
Member Author

greenrobot commented Feb 20, 2018

So we just double checked: the data.mdb file works across plaforms (x64, ARM, 32 and 64 bit). Thus, the FAQ is still valid.

@yocn
Copy link

yocn commented Mar 8, 2018

@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?

@greenrobot
Copy link
Member Author

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?

@greenrobot
Copy link
Member Author

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.

@greenrobot
Copy link
Member Author

Check 1.4.4 for BoxStoreBuilder.initialDbFile(...) which will provide a initial data file if it does not exist before.

@yocn
Copy link

yocn commented Mar 12, 2018

@greenrobot Really thanks, I'll check it later.

@yocn
Copy link

yocn commented Mar 12, 2018

@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?

@greenrobot
Copy link
Member Author

Looks good! Online JavaDocs are still to be updated...

@yocn
Copy link

yocn commented Mar 12, 2018

@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

@greenrobot
Copy link
Member Author

@yocn Will only be called if there's no DB yet.

@yocn
Copy link

yocn commented Mar 12, 2018

@greenrobot Yes~~~forget it , first run is normal, appreciate it.

@yizems
Copy link

yizems commented Aug 26, 2018

@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,

#310 (comment)

Sorry to disturb

@ErfolgreichCharismatisch
Copy link

ErfolgreichCharismatisch commented Dec 21, 2018

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?

@greenrobot
Copy link
Member Author

@ErfolgreichCharismatisch ObjectBox persists data across app starts of course.

@greenrobot-team
Copy link
Member

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

@xuan-nguyen-swe
Copy link

xuan-nguyen-swe commented Jan 9, 2019

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.

@basshelal
Copy link

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!

@Benjoyo
Copy link

Benjoyo commented Mar 28, 2019

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.

@greenrobot-team
Copy link
Member

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants