SQLiteOpenHelper multiple in-memory databases
Asked Answered
E

2

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?

Entente answered 4/5, 2016 at 18:12 Comment(1)
@OneCricketeer Considering the fact that you added an elaborated answer, I think it would be better to remove this misguiding comment now.Nipping
E
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

Explode answered 4/5, 2016 at 18:35 Comment(1)
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.Entente
M
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.

Manfred answered 4/5, 2016 at 18:40 Comment(1)
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.Entente

© 2022 - 2024 — McMap. All rights reserved.