2

Possible Duplicate:
Create SQLite DB in Android library

I am making a Java library and I need an SQLite database to be opened or created at runtime for my library to store data etc...

All the SQLite API for Android I've seen requires a context, from an activity to create a DB,since I'm doing a library, without activity etc... how can I get an SQLite DB ?

The problem is my library has only one public class, and inside my library in my inner workings I need to create a SQLite DB for private use. So I can't just ask the user to send in a context. Or can I ?

Just see for how does Google do it in their library ? At no point when I'm using the analytics library for Android do I pass anything other then an app id for analytics. And In their documentation you can read: "Pageviews, events and Ecommerce hits are stored in an SQLite database and dispatched to the Google Analytics servers periodically

0

1 Answer 1

3

how can I get an SQLite DB ?

Have the consumer of your library supply you with a Context.

So I can't just ask the user to send in a context. Or can I ?

Yes, you can. If you notice, many classes in the android.* packages have methods that take a Context as a parameter. You, too, can have methods that take a Context as a parameter.

Now, you will need to document your intended use of the Context and the lifespan of the objects that will touch it, so consumers of your library can pass in an appropriate one. For example, if your library will be creating a static data member that holds onto a SQLiteOpenHelper, you will want to actually have the consumer of the library supply you with an Application, not an Activity, Service, or other Context, so you do not run into garbage collection issues.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, that sounds great thank you, but in this case how does Google do it in their library ? At no point when I'm using the analytics library for Android do I pass anything other then an app id for analytics. And In their documentation you can read: "Pageviews, events and Ecommerce hits are stored in an SQLite database and dispatched to the Google Analytics servers periodically"
You do have to pass in a Context. The startNewSession method accepts two parameters: your web property id and your activity context: developers.google.com/analytics/devguides/collection/android/…
Oh great missed that ! Perfect, I'll get this and forward it to my database singleton class for SQLite dealings. But what do I need to import to use it in my library? Says Context is undefined. +1 upvote and accepted
@MrBean: Context is android.content.Context, as is indicated in the documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.