chessofnerd
chessofnerd

Reputation: 1279

What does the SQLiteOpenHelper class do with the context parameter?

I am extending the SQLiteOpenHelper class. My constructor is

public MyDatabaseHelper(Context context) {
    super(
        context,         // ???
        "MyDatabase.db", // Database name
        null,            // Cursor factory
        1                // database version
    );
}

What does the SQLiteOpenHelper constructor do with the context information?

For my application, the constructor will behave the same regardless of the program state (context). Can I pass null in for the context with out any future problems?

Upvotes: 6

Views: 4177

Answers (1)

Frank Leigh
Frank Leigh

Reputation: 5372

If you supply a null value, it will create an in-memory database instead but you'll need to supply null for the database name parameter as well so it's handled properly.

This is documented in the constructor documentation for context

context to use to open or create the database name of the database file, or null for an in-memory database

Also, if you view the source code of the SQLiteHelper class itself, you will see it uses the mName value to decide whether to use mContext. View the source code online here:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r1.2/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper.0mContext

Upvotes: 6

Related Questions