如何创建 sqllite3 内存数据库?

发布于 2024-07-09 07:50:07 字数 252 浏览 6 评论 0原文

适当用途之一 sqlite3 是“内存数据库”。 这听起来对我的 C++ 应用程序来说是一个非常有用的工具。 有谁有一个如何在 C 或 C++ 中完成此操作的示例吗? 我专门寻找一种规范的方法将多个平面文件放入内存数据库中,然后进行一些连接。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2024-07-16 07:50:07

这实际上很容易。 使用 C/C++ API 打开数据库时,只需指定“:memory:”作为数据库文件名。 这是引擎能够识别的特殊常数。 这实际上也适用于其他语言,例如 Python 或 Ruby,因为它们通常只包装 C/C++ API。 有关完整详细信息,请参阅 http://sqlite.org/c3ref/open.html

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.

离去的眼神 2024-07-16 07:50:07

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

您提到您想要读取几个平面文件并对它们进行联接。 如果可以将平面文件存储为 SQLite 数据库,则可以通过将一个文件附加到另一个数据库来直接使用这两个文件:

ATTACH foo.db AS foo

然后像这样引用 foo 中的表:

SELECT * FROM foo.users

这样您就可以进行连接,而无需创建 in-内存数据库。

Just open the file :memory: and that should do it (at least it does in 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:

ATTACH foo.db AS foo

Then refer to the tables in foo like so:

SELECT * FROM foo.users

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

终陌 2024-07-16 07:50:07

例如,如果您希望 SQLite 不使用临时文件作为日志,则除了手动请求连接、断开连接、附加或分离时之外,您不希望进行任何文件活动。 然后在连接到“:memory:”数据库后在运行时使用以下两个编译指示。

PRAGMA temp_store=MEMORY;
PRAGMA journal_mode=MEMORY;

来自文档。

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.

PRAGMA temp_store=MEMORY;
PRAGMA journal_mode=MEMORY;

From the docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文