Multi-database on Android

420 views
Skip to first unread message

Paolo

unread,
Oct 17, 2011, 11:03:06 AM10/17/11
to ormlite-android
Hi,

I'm interested to use ormlite to handle many db in Android.

I'm using a Activity group ( OrmLiteBaseActivityGroup ).

I've an activity on my group that doesn't extends ormlite, so in this
activity I want to switch between db. So, get all other activities and
I release the db helper ( whit releaseHelper() ).

I've a question. The protected releaseHelper:

protected void releaseHelper(H helper) {
OpenHelperManager.releaseHelper();
helper = null;
}

fail when the helper ( property of OrmLiteBaseActivityGroup ) must be
null ( cause helper is a copy of helper reference ( maybe use
this.helper = null ??????? ).

I'm wrong ?? ( If so, I'm sorry )..

Is, as your opinion, my trick to manage multi-db correct?

Many thanks,

Paolo

Paolo Mancini

unread,
Oct 19, 2011, 4:05:52 AM10/19/11
to ormlite-android
Any ideas??

Thank you,

Paolo

Gray Watson

unread,
Oct 21, 2011, 12:35:26 PM10/21/11
to ormlite...@googlegroups.com
On Oct 17, 2011, at 11:03 AM, Paolo wrote:

> Is, as your opinion, my trick to manage multi-db correct?

...

Sorry for the delay on this. This has been asked before but I've never taken a hard look at it. I've added yet another HelloAndroid application but this time called HelloAndroidTwoDbs. Here's the URL for the source:

www: http://ormlite.svn.sourceforge.net/viewvc/ormlite/examples/android/HelloAndroidTwoDbs/
svn: https://ormlite.svn.sourceforge.net/svnroot/ormlite/examples/android/HelloAndroidTwoDbs/

This piggybacks on the HelloAndroidNoBase project I added recently. It does not use the OrmLiteBaseActivity or other base classes at all and does not even use the OpenHelperManager manager. It creates two separate helpers using static methods on each of them. Each helper then manages each of the databases.

To use the helpers, you must call DatabaseHelper.getHelper(context). For every call to getHelper() from the Activity or Service as they start up, there should be a single corresponding call to close() when it is shutting down. If there are 3 calls to getHelper() in the application then there should be 3 calls to close(). The last call to close is when the helper and any associated database connections are shut down.

I'd be curious if anyone else is doing multiple databases and how they've handled it.
gray

Paolo Mancini

unread,
Oct 21, 2011, 1:22:10 PM10/21/11
to ormlite...@googlegroups.com
Thank you Mr. Gray...

I've a question....I have a list of db and when I select one of this I
want to switch ...

This is my solution...

I use an OrmLiteBaseActivityGroup, where all the childrens are Activity.

In each activity to get the helper I use the getHelper of the parent (
OrmLiteBaseActivityGroup instance )

When I want to switch beetween db, I close the helper of the
OrmLiteBaseActivityGroup instance and I reopen this passing the name
of the new Db on the constructor of the database helper.

Something like this:

public MyDatabaseHelper(Context context) {
super(context, getDbName(context), null, getDbVersion(context));
}

public static String getDbName(Context context)
{

I get the name in the Shared Preferences...where previous I saved the name
....

}

Is this a good or a bad idea?!

I hope that you will understand my bad english !!! Sorry :D


Thanks !


The name of the new db

So... If I use an OrmLiteBaseActivityGroup and to call the helper I
use only the getHelper() of the OrmLiteBaseActivityGroup instance, can
I

If I use an Orm

2011/10/21 Gray Watson <256...@gmail.com>:

David Fire

unread,
Oct 21, 2011, 1:22:58 PM10/21/11
to ormlite...@googlegroups.com
why do you use more than one dbs?

2011/10/21 Paolo Mancini <paol...@gmail.com>



--
 (\__/)
(='.'=)This is Bunny. Copy and paste bunny into your
(")_(")signature to help him gain world domination.

Paolo Mancini

unread,
Oct 21, 2011, 1:35:28 PM10/21/11
to ormlite...@googlegroups.com
Cause I'm making an app that can handle many db :D :D

Each db contains a configuration of the app

2011/10/21 David Fire <ddf...@gmail.com>:

David Fire

unread,
Oct 21, 2011, 1:37:30 PM10/21/11
to ormlite...@googlegroups.com
but why?
why no use just one db? or it is just an exercise ?
thanks

2011/10/21 Paolo Mancini <paol...@gmail.com>

Gray Watson

unread,
Oct 21, 2011, 1:38:37 PM10/21/11
to ormlite...@googlegroups.com
On Oct 21, 2011, at 1:22 PM, Paolo Mancini wrote:

> public MyDatabaseHelper(Context context) {
> super(context, getDbName(context), null, getDbVersion(context));
> }
>

> Is this a good or a bad idea?!
> I hope that you will understand my bad english !!! Sorry :D

Sorry Paolo but I just don't know if it is good or not. Seems like a complicated mechanism for sure but that doesn't necessarily mean that it is bad.

Best of luck,
gray

Paolo Mancini

unread,
Oct 21, 2011, 1:47:12 PM10/21/11
to ormlite...@googlegroups.com
Thank you Mr. Gray....

I'm sorry, but anyone answered my question:

Which is, as your opinion, the best way to handle many db ( not fixed
number, whit a unique DatabaseHandler ) using an app whit many
activities ?!?

2011/10/21 Gray Watson <256...@gmail.com>:

Paolo Mancini

unread,
Oct 21, 2011, 2:05:49 PM10/21/11
to ormlite...@googlegroups.com
Cause If I want to export one configuration, with one db it's not correct

I choose this way cause it's the unique solution to obtaing my goal

Thanks

2011/10/21 David Fire <ddf...@gmail.com>:

Filipe Leandro

unread,
Oct 21, 2011, 2:08:01 PM10/21/11
to ormlite...@googlegroups.com
What do you mean about configurations?
Couldn't you use the SharedPreferences class in android? Or is it a complex configuration model?

Filipe

Paolo Mancini

unread,
Oct 21, 2011, 2:42:18 PM10/21/11
to ormlite...@googlegroups.com
Yes,exactly, it's a complex configuration...I need to use many db..,in
addition I want to offer the possibility to export only a one
configuration between more devices with the same app

2011/10/21 Filipe Leandro <screami...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages