0

I have a requirement to create a new database for each user in app, and to have multiple databases that change on user switching. Does any of the existing ORM's like GreenDAO, Storio, DbFlow, Realm or similar orm/libraries for Android support this or is it better to do this on foot, define each table and fields by my self, and use Sqlitedbhelper to generate each database from scratch?

ddog
  • 670
  • 1
  • 10
  • 25
  • well, you can open realm instances with a given configuration in which you specify the realm's name; thus making it so that the user has its own realm.... that'll create a new realm file for whatever new name you provide – EpicPandaForce Nov 20 '15 at 22:32

2 Answers2

1

As StorIO developer, I will answer about support of multiple databases in StorIO:

It's possible, StorIO does not limit you in the number of databases you work with, just create as much StorIOSQLite instances as you need and pass unique SQLiteOpenHelper that'll work with different DBs to each instance of StorIOSQLite. The only recommendation from our team — use one instance of StorIOSQLite per DB.

According to the documentation, GreenDAO and DbFlow should be able to work with multiple databases.

Looks like Realm won't allow you to use multiple databases since it's a singleton in their SDK: Realm.getInstance(context). Edit: Realm can handle multiple DBs (see comments to the answer).

Artem Zinnatullin
  • 4,305
  • 1
  • 29
  • 43
  • It is totally possible to use multiple realm db instances in one app. You just have to provide different configurations when obtain a new realm instance (see "Configuring a Realm" https://realm.io/docs/java/latest/) – Roberto Artiles Astelarra Nov 21 '15 at 02:42
  • Artem do you have some examples on your repo for something similar to this? – ddog Nov 21 '15 at 12:39
  • @RobertoArtilesAstelarra sorry! I've fixed the answer. Great that Realm can do this. – Artem Zinnatullin Nov 21 '15 at 14:38
  • @ddog nope, but I don't see any problems with it. Just accept DB name in `SQLiteOpenHelper` constructor and you'll have one implementation of `SQLiteOpenHelper` for multiple DBs. – Artem Zinnatullin Nov 21 '15 at 14:39
  • Tnx for the answer, i will try with both greenDao and Storio and try to see witch one suits my needs the most – ddog Nov 21 '15 at 14:52
0

I've used greenDAO several times and this looks easy with it. You can take a look here.

Basically, you can specify by name what db do you want to use when you init the DaoMaster like this:

DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "your-db-name", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();

// After the initialization, you can get your DAO's and start working
SomeDao someDao = daoSession.getSomeDao();

I don't know if you are familiar with greenDAO, but all the needed classes are automatically generated using a generator module. You could find here a good tutorial explaining how to use it with Android Studio.

Jofre Mateu
  • 2,390
  • 15
  • 26
  • I have used GreenDao once a year ago, i see now that the 2.0 version is out, your answer seems nice, and i will take it in the consideration. – ddog Nov 21 '15 at 12:41