In-Memory Databases
The most common way to force an SQLite database to exist purely in memory is to open the database using the special filename “:memory:“. In other words, instead of passing the name of a real disk file into one of the sqlite3_open(), , or sqlite3_open_v2() functions, pass in the string “:memory:”. For example:
When this is done, no disk file is opened. Instead, a new database is created purely in memory. The database ceases to exist as soon as the database connection is closed. Every :memory: database is distinct from every other. So, opening two database connections each with the filename “:memory:” will create two independent in-memory databases.
The special filename “:memory:” can be used anywhere that a database filename is permitted. For example, it can be used as the filename in an command:
Note that in order for the special “:memory:” name to apply and to create a pure in-memory database, there must be no additional text in the filename. Thus, a disk-based database can be created in a file by prepending a pathname, like this: “./:memory:”.
- rc = sqlite3_open("file::memory:", &db);
Or,
In-memory databases are allowed to use shared cache if they are opened using a . If the unadorned “:memory:” name is used to specify the in-memory database, then that database always has a private cache and is this only visible to the database connection that originally opened it. However, the same in-memory database can be opened by two or more database connections as follows:
- rc = sqlite3_open("file::memory:?cache=shared", &db);
Or,
This allows separate database connections to share the same in-memory database. Of course, all database connections sharing the in-memory database need to be in the same process. The database is automatically deleted and memory is reclaimed when the last connection to the database closes.
Or,
- ATTACH DATABASE 'file:memdb1?mode=memory&cache=shared' AS aux1;
When an in-memory database is named in this way, it will only share its cache with another connection that uses exactly the same name.
Temporary Databases
When the name of the database file handed to or to ATTACH is an empty string, then a new temporary file is created to hold the database.
- rc = sqlite3_open("", &db);
A different temporary file is created each time, so that just like as with the special “:memory:” string, two database connections to temporary databases each have their own private database. Temporary databases are automatically deleted when the connection that created them closes.
The previous paragraphs describe the behavior of temporary databases under the default SQLite configuration. An application can use the and the SQLITE_TEMP_STORE compile-time parameter to force temporary databases to behave as pure in-memory databases, if desired.