Skip to content

Data export and import #310

Open
Open

Description

@greenrobot

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.

Activity

  1. yocn commented on Dec 13, 2017

    @yocn

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

  2. motornyimaksym commented on Jan 23, 2018

    @motornyimaksym

    Extremely useful feature! Looking forward!

  3. bigntallmike commented on Jan 25, 2018

    @bigntallmike

    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.

  4. motornyimaksym commented on Jan 26, 2018

    @motornyimaksym

    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.

  5. yocn commented on Jan 31, 2018

    @yocn

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

  6. motornyimaksym commented on Jan 31, 2018

    @motornyimaksym

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

  7. yocn commented on Feb 6, 2018

    @yocn

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

  8. greenrobot-team commented on Feb 13, 2018

    @greenrobot-team
    Member

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

  9. greenrobot commented on Feb 20, 2018

    @greenrobot
    MemberAuthor

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

  10. yocn commented on Mar 8, 2018

    @yocn

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

  11. greenrobot commented on Mar 8, 2018

    @greenrobot
    MemberAuthor

    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?

  12. greenrobot commented on Mar 8, 2018

    @greenrobot
    MemberAuthor

    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.

  13. greenrobot commented on Mar 8, 2018

    @greenrobot
    MemberAuthor

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

  14. yocn commented on Mar 12, 2018

    @yocn

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

  15. yocn commented on Mar 12, 2018

    @yocn

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

  16. greenrobot commented on Mar 12, 2018

    @greenrobot
    MemberAuthor

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

  17. yocn commented on Mar 12, 2018

    @yocn

    @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

  18. greenrobot commented on Mar 12, 2018

    @greenrobot
    MemberAuthor

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

  19. yocn commented on Mar 12, 2018

    @yocn

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

  20. yizems commented on Aug 26, 2018

    @yizems

    @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

  21. ErfolgreichCharismatisch commented on Dec 21, 2018

    @ErfolgreichCharismatisch

    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?

  22. greenrobot commented on Dec 27, 2018

    @greenrobot
    MemberAuthor

    @ErfolgreichCharismatisch ObjectBox persists data across app starts of course.

  23. greenrobot-team commented on Jan 7, 2019

    @greenrobot-team
    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

  24. xuanswe commented on Jan 9, 2019

    @xuanswe

    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.

  25. basshelal commented on Feb 4, 2019

    @basshelal

    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!

  26. Benjoyo commented on Mar 28, 2019

    @Benjoyo

    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.

  27. greenrobot-team commented on Apr 1, 2019

    @greenrobot-team
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions