使用飞镖将sqlite数据库加载到内存中?
Load sqlite database into memory using dart?
是否可以完全从内存中加载 sqlite 数据库?
我正在使用 sqflite 库:
https://pub.dev/packages/sqflite
打开数据库时,它需要一个文件路径:
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');
我只从数据库中读取,从不写入。我想从文件中读取数据库,然后 sql 不再打开文件。
有一个 inMemoryDatabasePath 属性:
https://pub.dev/documentation/sqflite_common/latest/sqlite_api/inMemoryDatabasePath-constant.html
但这似乎只是为了在内存中创建一个新的空数据库。
所以,我的问题是是否可以完全打开一个 sqlite 数据库并将其加载到内存中?
如果没有,是否可以创建一个内存数据库,然后将所有数据复制到其中?与此类似:
How to copy a sqlite table from a disk database to a memory database in python?
(如果是这样,谁知道 dart / sqflite 中等效的 sql 命令是什么)?
我从这个 post 中找到了解决方案:
https://whosebug.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();
是否可以完全从内存中加载 sqlite 数据库?
我正在使用 sqflite 库:
https://pub.dev/packages/sqflite
打开数据库时,它需要一个文件路径:
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');
我只从数据库中读取,从不写入。我想从文件中读取数据库,然后 sql 不再打开文件。
有一个 inMemoryDatabasePath 属性: https://pub.dev/documentation/sqflite_common/latest/sqlite_api/inMemoryDatabasePath-constant.html
但这似乎只是为了在内存中创建一个新的空数据库。
所以,我的问题是是否可以完全打开一个 sqlite 数据库并将其加载到内存中?
如果没有,是否可以创建一个内存数据库,然后将所有数据复制到其中?与此类似:
How to copy a sqlite table from a disk database to a memory database in python?
(如果是这样,谁知道 dart / sqflite 中等效的 sql 命令是什么)?
我从这个 post 中找到了解决方案: https://whosebug.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();