Ajay
Ajay

Reputation: 1826

Using multiple database helpers in Android OrmLite

My app uses two databases. To handle these databases I have created two DatabaseHelper classes, say class X and class Y.

I use the respective getHelper() methods to fetch the required database helper.

But when the initial helper is X and I need Y, I use this following code:

OpenHelperManager.releaseHelper();
dbHelper = OpenHelperManager.getHelper(context, Y.class)

And I get the following exception:

java.lang.IllegalStateException: Helper class was class X but is trying to be reset to class Y

I know that in OrmLite we can use a single instance of the helper with 1 database connection, and I don't think I am violating that rule. Can anyone help here, please?

Upvotes: 1

Views: 3215

Answers (1)

jmg
jmg

Reputation: 586

It looks like you are reusing the same variable dbHelper for both classes (class X and Y) but the type of dbHelper is not a parent class of X and Y. I would need to see more of your code, but it seems that this error has nothing to do with ormlite. You can either:

  • Use a separate variable per helper (you will have some logic anyway to decide which database you want to access).
  • Create an abstract parent class for classes X and Y and change the type of dbHelper to that.

Btw, here you can see an example of using two databases with ormlite for android.

Upvotes: 2

Related Questions