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)?

mikechambers
  • 3,047
  • 3
  • 24
  • 31

1 Answers1

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();
mikechambers
  • 3,047
  • 3
  • 24
  • 31