1

Is it possible to load a sqlite database in dart completely from memory?

I am using the sqflite library:

https://pub.dev/packages/sqflite

When opening a database it takes a path to a file:

var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');

I am only reading from the database, never writing to it. I would like to read the database from the file, and then not have sqlite have the file open anymore.

There is an inMemoryDatabasePath property: https://pub.dev/documentation/sqflite_common/latest/sqlite_api/inMemoryDatabasePath-constant.html

but that appears to only be for creating a new empty database in memory.

So, my question is whether it is possible to completely open a sqlite database and load it into memory?

If not, would it be possible to create an in-memory database, and then copy all of the data into it? similar to this:

How to copy a sqlite table from a disk database to a memory database in python?

(if so, anyone know what would be the equivalent sql commands in dart / sqflite)?

1 Answer 1

2

I found the solution from this post: https://stackoverflow.com/posts/10506228/revisions

    sqfliteFfiInit();
    
    OpenDatabaseOptions options = OpenDatabaseOptions(readOnly: true);
    Databse _db = await databaseFactoryFfi.openDatabase(inMemoryDatabasePath);
    
    Database tmp = await databaseFactoryFfi.openDatabase(dbFile.path, options: options);
    
    await _db.rawQuery("ATTACH DATABASE ? as tmpDb", [dbFile.path]);
    await _db.rawQuery("CREATE TABLE TABLENAME AS SELECT * FROM tmpDb.TABLENAME");
    
    tmp.close();
Sign up to request clarification or add additional context in comments.

Comments

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.