SQLite tutorial (10): Memory Database and temporary database, sqlite tutorial
I. Memory Database:
In SQLite, databases are usually stored in disk files. However, in some cases, we can keep the database in the memory. The most common method is to pass ": memory:" To the database file name parameter when calling sqlite3_open (), for example:
Copy codeThe Code is as follows:
Rc = sqlite3_open (": memory:", & db );
After the above functions are called, no disk files are generated. Instead, a new database is successfully created in pure memory. Because the database is not persistent, the database disappears immediately after the current database connection is closed. Note that although multiple database connections can be used to create memory databases, they are different databases and have no relationship with each other. In fact, we can also use the Attach command to Attach a memory database to the current connection like other common databases, such:
Copy codeThe Code is as follows:
Attach database ': memory:' AS aux1;
Ii. Temporary database:
When you call the sqlite3_open () function or execute the ATTACH command, if the database file parameter is a Null String, a new temporary file will be created as the underlying file of the temporary database, such:
Copy codeThe Code is as follows:
Rc = sqlite3_open ("", & db );
Or
Copy codeThe Code is as follows:
Attach database ''AS aux2;
Similar to a memory database, the temporary databases created by the two databases are also independent. When the connection is closed, the temporary database will automatically disappear and the underlying files will also be deleted.
Although the disk file is created to store data information in the temporary database, the temporary database will usually reside in the memory like the memory database. The only difference is that, when the data volume in the temporary database is too large, SQLite writes some data in the temporary database to the disk file to ensure that more memory is available for other operations, memory databases always store data in the memory.