You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by Maksim Motorny, sgffsg, Chiaradia Juan Martín, ali-abbas-10p, zhaoyingkun, Martin Hlavňa, Petr, yizems, Ahmed Hamdy, Eduardo Vieira Mendes and 7 more
Reacted by Michael T. Babcock, zhaoyingkun, sgffsg, Hatzen and Brandon Olson
Reacted by Maksim Motorny, ali-abbas-10p, zhaoyingkun, sgffsg, Roger Ou, Hatzen, Brandon Olson and lax_man
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.
@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?
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.
@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
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?
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.
Reacted by Joielechong, Bilux, dev-thinks and yomigatz
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!
Reacted by Scott Kruse, Joielechong, Hatzen, yuanliwei and rlee2365
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.
Activity
yocn commentedon Dec 13, 2017
Wow~ Could you please share some time schedule for this feature? @greenrobot
motornyimaksym commentedon Jan 23, 2018
Extremely useful feature! Looking forward!
bigntallmike commentedon Jan 25, 2018
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.
motornyimaksym commentedon 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 commentedon Jan 31, 2018
@Yazon2006 where are you find the FAQ? would you please share the url?
motornyimaksym commentedon Jan 31, 2018
@yocn in very unexpected place: http://objectbox.io/faq/
yocn commentedon Feb 6, 2018
@greenrobot So, Is the Can I ship my app with a pre-built database? in FAQ recommend for developers?
greenrobot-team commentedon Feb 13, 2018
@yocn Yes? Not sure what you are getting at with recommended. -ut
greenrobot commentedon 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 commentedon 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 commentedon Mar 8, 2018
Yes, this is mostly correct. Just a few comments:
context.getFilesDir()(don't do "data/data/myPackageName/file")BoxStoreBuilder.getAndroidDbDir(), you can take as referenceI 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 commentedon Mar 8, 2018
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 commentedon Mar 8, 2018
Check 1.4.4 for
BoxStoreBuilder.initialDbFile(...)which will provide a initial data file if it does not exist before.yocn commentedon Mar 12, 2018
@greenrobot Really thanks, I'll check it later.
yocn commentedon 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:Is My invoke method correct?
greenrobot commentedon Mar 12, 2018
Looks good! Online JavaDocs are still to be updated...
yocn commentedon Mar 12, 2018
@greenrobot but I cannot get the
provide()callback inFactory<InputStream>,LogUtil.d("yocn Factory<InputStream> provide");cannot be printed, I don't know where I did wrong or it is normal behaviorgreenrobot commentedon Mar 12, 2018
@yocn Will only be called if there's no DB yet.
yocn commentedon Mar 12, 2018
@greenrobot Yes~~~forget it , first run is normal, appreciate it.
yizems commentedon 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 commentedon Dec 21, 2018
Do I get this right, you are creating your databases on runtime only, so each
onCreateyou have to create your database? Or do you save your database somewhere internally once programmatically but you cannot just edit it outside in something likeSQLite Database Browserand 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 commentedon Dec 27, 2018
@ErfolgreichCharismatisch ObjectBox persists data across app starts of course.
greenrobot-team commentedon Jan 7, 2019
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
xuanswe commentedon 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 pluginio.objectbox, so it can convert the provideddata.jsontodata.mdb. Then in the app, we can use thisdata.mdbas preloaded data.basshelal commentedon Feb 4, 2019
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 commentedon Mar 28, 2019
I keep getting this
io.objectbox.exception.DbException: Incoming entity ID 1:XXX does not match existing UID XYZexception 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 commentedon Apr 1, 2019
@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