如何在特定地址创建SQLite内存数据库

Mic*_*lGG 0 sqlite mmap ramdisk in-memory-database

我想使用 SQLite 在文件中存储一些元数据信息。该文件已经进行了 mmap。我想要做的是创建一个 SQLite DB,传入一个 char* 来告诉它在哪里创建 DB,而不是让它分配自己的 DB。这可以做到吗?内存数据库的 SQLite 文档只是说使用“:内存:”,并且数据库将在进程结束时被销毁,没有指示如何使用已经存在的数据或保留它。

如果没有,Linux 上有哪些解决方法?是否有“反向”mmap,以便我可以获取地址并将其映射到新文件?(那么 /foo 将是 /bar 的一部分的视图?)

mic*_*ion 5

VFS 绝对是可行的方法,但您甚至不需要编写自己的:sqlite3 发行版包含一个ext/misc/memvfs.c已经实现您需要的文件:

USAGE:

sqlite3_open_v2("file:/whatever?ptr=0xf05538&sz=14336&max=65536", &db,
                   SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI,
                   "memvfs");

These are the query parameters:

   ptr=          The address of the memory buffer that holds the database.

   sz=           The current size the database file

   maxsz=        The maximum size of the database.  In other words, the
                 amount of space allocated for the ptr= buffer.

   freeonclose=  If true, then sqlite3_free() is called on the ptr=
                 value when the connection closes.

The ptr= and sz= query parameters are required.  If maxsz= is omitted,
then it defaults to the sz= value.  Parameter values can be in either
decimal or hexadecimal.  The filename in the URI is ignored.
Run Code Online (Sandbox Code Playgroud)