SQLite Forum

FILE_SHARE_DELETE Flag
Login

FILE_SHARE_DELETE Flag

(1) By anonymous on 2021-03-21 11:16:35 [link] [source]

Usecase

I like it when applications come as one big executable instead of many small files.

Because of that I have included my sqlite database with the default settings directly into the binary. It gets written on program start and then opened. It is read-only.

I'd like to immediately delete the file after opening it so that the OS will take care of removing the file in case the application crashes or a power outage or whatever.

Problem

I can do so on Unix like OSes, Linux, MacOS work fine. But windows usually doesn't allow deletion of an open file.

Solution

I made the following change. In file os_win.c change this

dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;

to this

dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;

This now allows identical handling on Windows as well.

I have seen multiple discussions about this in the mailing list but no official comment whether something like this might be included directly in SQLite3. Or if there are any adverse effects that I don't know about that prevents inclusion.

Thanks

Andreas

(2) By anonymous on 2021-03-21 14:24:28 in reply to 1 [source]

I have included my sqlite database with the default settings directly into the binary. It gets written on program start and then opened. It is read-only.

I think sqlite3_deserialize would be better solution to your problem. You don't need to write the db to disk to read it.

(3) By Clemens Ladisch (cladisch) on 2021-03-21 16:24:44 in reply to 1 [link] [source]

When you write the DB into a file, the data appears twice in the file cache (executable and temp file). A better solution for your problem would be to use a VFS that allows to read DB pages directly from the executable file: https://www.sqlite.org/src/file/ext/misc/appendvfs.c