Mario
Mario

Reputation: 14780

Sqlite Move Database to Memory in C#

How can I move an sqlite disk database to memory in C# ?

I need to open a database from disk to an in-memory database for faster operations and when it finishes the work then save it back to file.

The disk database is an empty database, I want to transfer the data from a List to the memory database and use this to sort very large data which kills the List.Sort function. After the data is sorted I don't need the database anymore.

Upvotes: 0

Views: 2542

Answers (1)

Ali Bahrami
Ali Bahrami

Reputation: 6073

I'm afraid you cannot do that. And I do not recommend that, Why don't you just increase the cache size?

An SQLite database is normally stored in a single ordinary disk file. However, in certain circumstances, the database might be stored in memory.

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(), sqlite3_open16(), or sqlite3_open_v2() functions, pass in the string ":memory:". For example:

And every connection to SQlite creates a seperated new database.

rc = sqlite3_open(":memory:", &db); 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.

If you are going to implement such a thing, there are not any official way to do it! But you can:

  1. Open a new in-memory db
  2. Copy all data from file db to memory
  3. Compare and merge new/deleted/updated data from memory to disk

Here is code in C language as an example for opening a SQLite file as an in-memory:

https://www.mail-archive.com/[email protected]/msg15929.html

Upvotes: 1

Related Questions