Pass the DB name to the Helper class

12 views
Skip to first unread message

Paolo

unread,
Jun 7, 2011, 4:29:49 AM6/7/11
to ormlite-android
Hi,

I'm developing with Android and I need to pass the DB name to the
Helper class. , not as the examples which defines the name in a static
private member on the helper class.

In particular, I need to get the Db name from the Shared Preferences
of the app, but the OrmLiteSqliteOpenHelper can't acces to the Shared
Preferences, and for this I need to pass in some way this string.

Is it possible with OrmLite ?

Thanks

Gray Watson

unread,
Jul 8, 2011, 5:05:19 PM7/8/11
to ormlite...@googlegroups.com
On Jun 7, 2011, at 4:29 AM, Paolo wrote:

> I'm developing with Android and I need to pass the DB name to the

> Helper class, not as the examples which defines the name in a static


> private member on the helper class.
>
> In particular, I need to get the Db name from the Shared Preferences
> of the app, but the OrmLiteSqliteOpenHelper can't acces to the Shared
> Preferences, and for this I need to pass in some way this string.

Is this really true Paolo? The open helper has access to the Context and you can do a getSharedPreferences(...):

http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29

In your DatabaseHelper class, the constructor gets passed the Context and you should be able to create a static method to extract the database name from the Context and pass it up to the OrmLiteSqliteOpenHelper constructor. Something like:

public DatabaseHelper(Context context) {
super(context, extractDatabaseName(context), null, DATABASE_VERSION);
}

private static String extractDatabaseName(Context context) {
SharedPreferences prefs = context.getSharedPreferences(..., ...);
return prefs.getString(..., ...);
}

Won't that work? Anyone else want to comment?
gray

Reply all
Reply to author
Forward
0 new messages