简体   繁体   中英

How do I create a sqllite3 in-memory database?

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.

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.

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.

If you want SQLite to not use temporary files as journals, eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM