SQLite Forum

In-memory database from sqlite file
Login
I have recently done exactly this and can tell you that Serialize and Deserialize work fantastically well to achieve this.

The things you need to consider are:

1. To get the DB file INTO Serialized form, you need to open it with an SQLite connection and tell the connection to [Serialize it to bytes](https://sqlite.org/c3ref/serialize.html) - which means you can do it both from a physical disk-file or from an in-memory DB to start with.
 - I believe you *can* just read the file-bytes too (I haven't tested it) but then you have no control over the file state and whether it is open, has roll-back journals, etc. A connection is the way to know for sure you get a good serialized byte-stream and, from your description, it sounds like what your use-case needs.
 - You can of course put these bytes into a file again or a pipe or data transport, etc.

2. To [Deserialize the bytes](https://sqlite.org/c3ref/deserialize.html) into memory, you first have to create and open an SQLite connection for a normal In-Memory DB, then give that connection as the Deserialization target DB, along with some flags and the bytes. This bit is pretty straight forward.

3. You have to compile SQLite with [SQLITE_ENABLE_DESERIALIZE - The Serialize/Deserialize switch](https://sqlite.org/compile.html#enable_deserialize). It's not on by default.


And that's it. We use this for multiple very fast uploads to a central station, which push the received bytes into in-memory DBs and extract data as needed. It's very easy, very fast and works very well.