如何创建一个sqllite3内存数据库?

时间:2023-01-15 09:22:27

One of the appropriate uses for sqlite3 is "in-memory databases". This sounds like a really useful tool for my C++ applications. Does anyone have an example of how this is done in C or C++? I'm specifically looking for a canonical way to slurp several flat-files into an in-memory database, then do some joins.

sqlite3的一个适当用途是“内存数据库”。对于我的c++应用程序来说,这是一个非常有用的工具。有没有人举过用C或c++实现这个功能的例子?我特别在寻找一种规范的方法,将几个平面文件拖放到内存中的数据库中,然后进行一些连接。

3 个解决方案

#1


16  

It's actually quite easy. Just specify ':memory:' as the database filename when opening a database using the C/C++ API. It's a special constant that the engine will recognize. The same actually works for other languages such as Python or Ruby, since they typically just wrap the C/C++ API. See http://sqlite.org/c3ref/open.html for complete details.

这其实很简单。使用C/ c++ API打开数据库时,只需指定':memory:'作为数据库文件名。这是一个引擎能识别的特殊常数。同样的方法也适用于其他语言,如Python或Ruby,因为它们通常只封装C/ c++ API。详细信息请参见http://sqlite.org/c3ref/open.html。

#2


5  

Just open the file :memory: and that should do it (at least it does in PHP).

只要打开文件:memory:就可以了(至少在PHP中是这样)。

You mention that you want to read in several flat files and do joins on them. If it's possible to store the flat files as SQLite databases, you can work directly with both by attaching one to the other:

您提到您希望在几个平面文件中进行读取,并对它们进行连接。如果可以将平面文件存储为SQLite数据库,您可以通过将一个文件附加到另一个文件来直接处理这两个文件:

ATTACH foo.db AS foo

Then refer to the tables in foo like so:

然后参照foo中的表,如:

SELECT * FROM foo.users

This way you can do your joins without the need for creating an in-memory database.

这样,您就可以进行连接,而不需要创建内存中的数据库。

#3


5  

If you want SQLite to not use temporary files as journals, e.g, you don't want any file activity other than when you manually requests a connect, disconnect, attach or detach. Then use the following two pragmas at runtime after you connect to your ":memory:" database.

如果您希望SQLite不使用临时文件作为日志,e。除了手动请求连接、断开连接、附加或分离之外,您不需要任何文件活动。然后在你连接到你的“:内存:”数据库后,在运行时使用以下两个pragmas。

PRAGMA temp_store=MEMORY;
PRAGMA journal_mode=MEMORY;

From the docs.

从文档。

#1


16  

It's actually quite easy. Just specify ':memory:' as the database filename when opening a database using the C/C++ API. It's a special constant that the engine will recognize. The same actually works for other languages such as Python or Ruby, since they typically just wrap the C/C++ API. See http://sqlite.org/c3ref/open.html for complete details.

这其实很简单。使用C/ c++ API打开数据库时,只需指定':memory:'作为数据库文件名。这是一个引擎能识别的特殊常数。同样的方法也适用于其他语言,如Python或Ruby,因为它们通常只封装C/ c++ API。详细信息请参见http://sqlite.org/c3ref/open.html。

#2


5  

Just open the file :memory: and that should do it (at least it does in PHP).

只要打开文件:memory:就可以了(至少在PHP中是这样)。

You mention that you want to read in several flat files and do joins on them. If it's possible to store the flat files as SQLite databases, you can work directly with both by attaching one to the other:

您提到您希望在几个平面文件中进行读取,并对它们进行连接。如果可以将平面文件存储为SQLite数据库,您可以通过将一个文件附加到另一个文件来直接处理这两个文件:

ATTACH foo.db AS foo

Then refer to the tables in foo like so:

然后参照foo中的表,如:

SELECT * FROM foo.users

This way you can do your joins without the need for creating an in-memory database.

这样,您就可以进行连接,而不需要创建内存中的数据库。

#3


5  

If you want SQLite to not use temporary files as journals, e.g, you don't want any file activity other than when you manually requests a connect, disconnect, attach or detach. Then use the following two pragmas at runtime after you connect to your ":memory:" database.

如果您希望SQLite不使用临时文件作为日志,e。除了手动请求连接、断开连接、附加或分离之外,您不需要任何文件活动。然后在你连接到你的“:内存:”数据库后,在运行时使用以下两个pragmas。

PRAGMA temp_store=MEMORY;
PRAGMA journal_mode=MEMORY;

From the docs.

从文档。