Any way to open sqlite database from in-memory data?
(1) By martin2020 on 2025-03-06 22:05:02 [link] [source]
In a server that I am writing, I am receiving a small uploaded database file, and I thought it might be a good idea to keep the uploaded file in memory, rather than saving it to disk. Can I open this sqlite database without writing it to disk at all?
I thought that perhaps using fmemopen to create a FILE pointer to the data might allow me to use the file with sqlite, but I am only seeing the sqlite open methods that take a filename. Is there any way to open a sqlite database from a FILE* or from a data buffer directly?
(2.2) By martin2020 on 2025-03-06 22:39:47 edited from 2.1 in reply to 1 [link] [source]
EDIT: it seems that files made with mkstemp() are not automatically deleted on program exit, as files created with tempfile() are, so I guess I am still curious about a solution more similar to my original thought.
(3) By Roy Hashimoto (rhashimoto) on 2025-03-07 00:33:07 in reply to 1 [link] [source]
Is there any way to open a sqlite database from a FILE* or from a data buffer directly?
You can do this with a custom VFS. It wouldn't be difficult to write one yourself (a lot of the methods would be no-ops), but it looks like there already is one that works with a memory buffer.
(5) By Nuno Cruces (ncruces) on 2025-03-07 00:50:27 in reply to 3 [link] [source]
You can also use sqlite3_deserialize
.
If your build does not omit serialization (with SQLITE_OMIT_DESERIALIZE
), the memvfs
Roy suggested comes bundled, as it is used to implement sqlite3_deserialize
.
(8) By martin2020 on 2025-03-07 01:34:14 in reply to 5 [link] [source]
Good to know; thanks for the info.
(7) By martin2020 on 2025-03-07 01:33:17 in reply to 3 [source]
Great! Thanks for pointing me to memvfs. That looks like exactly what I was looking for.
(4) By Rowan Worth (sqweek) on 2025-03-07 00:45:39 in reply to 1 [link] [source]
See also sqlite3_serialize and sqlite3_deserialize which can be used for this purpose.
It seems that they require an active sqlite3*
connection already so you might have to start by opening an in-memory DB to avoid touching disk; I'm not exactly sure of the correct sequence of events.
(10) By Richard Hipp (drh) on 2025-03-07 11:47:23 in reply to 4 [link] [source]
The serialize/deserialize interface was designed to be used for this purpose.
An example use case for serialize/deserialize is the patch command of
the Fossil Version Control System - the software that
is running this Forum and that was designed and written for management of the
SQLite source code. The SQLite devs use "fossil patch pull" and/or "fossil patch push"
frequently. For example if I'm making a change to SQLite, working on my Linux
desktop, and I want to make sure the change works on Windows before I commit,
I move to my Windows-11 machine and type: "fossil patch pull desktop:sqlite/sqlite
"
to cause all of the uncommitted changes at "desktop:sqlite/sqlite" to be
"serialized" into an SQLite database, sent over an SSH channel to the Win11
machine, then "deserialized", interpreted,and applied to the local checkout,
whence they can be tested.
It turns out that SQLite database files make an excellent wire protocol for sending structured data that is a mix of textual, numeric, and binary content. More conventional encodings like JSON or XML do not work directly with binary content. You'd have to hex-encode (or similar) your binary content for use with JSON or XML. But SQLite databases handle binary without issue. This is what makes an SQLite database such a convenient wrapper for sending patches.
Usage Hint: The size of an SQLite database is minimized by setting the page-size to 512.
(6) By cj (sqlitening) on 2025-03-07 01:08:23 in reply to 1 [link] [source]
All that is needed is to save download so why is this a "good idea?" -- file to be downloaded slOpen "test.db3","C" slexe "create table if not exists t1(c1 text)" slexebind "insert into t1 values(?)",bt("This is my only data") slclose -- simulate download OPEN "test.db3" FOR BINARY AS #1 GET$ #1,LOF(1),sBuffer CLOSE #1 -- save sBuffer to a database file OPEN "test2.db3" FOR OUTPUT AS #1 PRINT #1,sBuffer; CLOSE #1 END FUNCTION
(9) By martin2020 on 2025-03-07 01:44:58 in reply to 6 [link] [source]
In my case, I will be extracting the data contained in the database and using it in response to the upload request, so it doesn't make sense for the uploaded file to outlive the request. To ensure I am not leaving behind a file that is written to disk for every request, I thought it would be good to simply never write the uploaded file to disk.
(11) By Simon Slavin (slavin) on 2025-03-07 14:06:55 in reply to 1 [link] [source]
I don't know why nobody has pointed you to
https://www.sqlite.org/inmemorydb.html
which seesm to to what you want.