如何使用SQLite从内存中打开数据库?

时间:2022-11-11 16:50:21

It looks like all the methods for loading SQLite involve loading from a named file using a string. I would like to load SQlite database from memory.

看起来加载SQLite的所有方法都涉及使用字符串从命名文件加载。我想从内存加载SQlite数据库。

The database is already loaded into memory.

数据库已加载到内存中。

2 个解决方案

#1


10  

Use a special file name, :memory:

使用特殊文件名:memory:

sqlite3_open(":memory:", &db);

libsqlite must have been compiled without SQLITE_OMIT_MEMORYDB defined, as pointed out in SQLite documentation:

如SQLite文档中所指出的,必须在没有定义SQLITE_OMIT_MEMORYDB的情况下编译libsqlite:

SQLITE_OMIT_MEMORYDB

When this is defined, the library does not respect the special database name ":memory:" (normally used to create an in-memory database). If ":memory:" is passed to sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(), a file with this name will be opened or created.

定义它时,库不遵循特殊数据库名称“:memory:”(通常用于创建内存数据库)。如果将“:memory:”传递给sqlite3_open(),sqlite3_open16()或sqlite3_open_v2(),则将打开或创建具有此名称的文件。

If you want to read the database that is already fully loaded into memory however, it will be more work. You will have to implement a custom VFS layer to operate on memory files and register it with your SQLite context.

如果你想读取已经完全加载到内存中的数据库,那将会更有效。您必须实现一个自定义VFS层来操作内存文件并将其注册到您的SQLite上下文中。

See:

I have not implemented it myself, so I can't reliably tell whether you have to implement the entire new VFS layer, or you can get away with substituting some functions in the default one (latter is unlikely).

我自己没有实现它,所以我不能可靠地判断你是否必须实现整个新的VFS层,或者你可以在默认的一个函数中替换一些函数(后者不太可能)。

#2


1  

There is a memvfs implementation at

有一个memvfs实现在

article.gmane.org/gmane.comp.db.sqlite.general/46450

#1


10  

Use a special file name, :memory:

使用特殊文件名:memory:

sqlite3_open(":memory:", &db);

libsqlite must have been compiled without SQLITE_OMIT_MEMORYDB defined, as pointed out in SQLite documentation:

如SQLite文档中所指出的,必须在没有定义SQLITE_OMIT_MEMORYDB的情况下编译libsqlite:

SQLITE_OMIT_MEMORYDB

When this is defined, the library does not respect the special database name ":memory:" (normally used to create an in-memory database). If ":memory:" is passed to sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(), a file with this name will be opened or created.

定义它时,库不遵循特殊数据库名称“:memory:”(通常用于创建内存数据库)。如果将“:memory:”传递给sqlite3_open(),sqlite3_open16()或sqlite3_open_v2(),则将打开或创建具有此名称的文件。

If you want to read the database that is already fully loaded into memory however, it will be more work. You will have to implement a custom VFS layer to operate on memory files and register it with your SQLite context.

如果你想读取已经完全加载到内存中的数据库,那将会更有效。您必须实现一个自定义VFS层来操作内存文件并将其注册到您的SQLite上下文中。

See:

I have not implemented it myself, so I can't reliably tell whether you have to implement the entire new VFS layer, or you can get away with substituting some functions in the default one (latter is unlikely).

我自己没有实现它,所以我不能可靠地判断你是否必须实现整个新的VFS层,或者你可以在默认的一个函数中替换一些函数(后者不太可能)。

#2


1  

There is a memvfs implementation at

有一个memvfs实现在

article.gmane.org/gmane.comp.db.sqlite.general/46450