7

android.database.sqlite.SQLiteOpenHelper provides the ability to use an in-memory database if the name argument to its constructor is null:

String: of the database file, or null for an in-memory database

If SQLiteOpenHelper is instantiated multiple times with a null name argument, do they access the same in-memory database or is a separate in-memory database created each time?

1
  • @OneCricketeer Considering the fact that you added an elaborated answer, I think it would be better to remove this misguiding comment now. Commented Feb 16, 2021 at 10:10

2 Answers 2

8

From SQLite official documentation In-Memory Databases

Opening two database connections each with the filename ":memory:" will create two independent in-memory databases.

In Android, pass null instead of ":memory:"

So, If you instantiate SQLiteOpenHelper multiple times with a null name argument then it create separate in-memory database created each time

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

1 Comment

See @cricket_007's answer for more details since that got posted while I was researching this. SQLiteDatabaseConfiguration.MEMORY_DB_PATH = ":memory:" which is referenced from SQLiteDatabase.create to create a memory backed database. This passes through to openDatabase which instantiates SQLiteDatabase with the ":memory:" path.
3

If we look at the source code, we see that in the constructor mName would get set to null.

public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
        DatabaseErrorHandler errorHandler) {
    if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);

    mContext = context;
    mName = name;
    mFactory = factory;
    mNewVersion = version;
    mErrorHandler = errorHandler;
}

Which means getDatabaseName() returns null.

public String getDatabaseName() {
    return mName;
}

Later, through the use of getReadableDatabase() or getWritableDatabase(), if mName is null, then it calls the create method for an in-memory database instead of trying to opening one from disk.

if (mName == null) {
    db = SQLiteDatabase.create(null); // in-memory
} else {
    // db file opened or created
}
... 
return db;

That db variable is maintained in the SQLiteOpenHelper until it is closed, which in the case of an in-memory database, means the data is deleted.


To clarify,

Each instance of a SQLiteOpenHelper that uses an in-memory database will its own database while the same instance will use one database and persist that data until it is closed.

1 Comment

Thanks for researching the source. Combined with @USKMobility's answer, it looks like calls to the same SQLiteOpenHelper would yield the same database (assuming it hasn't been closed) and calls to different instances of SQLiteOpenHelper would create and access separate in memory databases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.