SQLite Forum

ghost file "file" when using URI for memory database with SQLITE_USE_URI=0

ghost file "file" when using URI for memory database with SQLITE_USE_URI=0

(1) By francois (fcsqlite) on 2022-01-24 15:07:32 [link] [source]


using a URI scheme to open an in-memory connection with the compilation option off (SQLITE_USE_URI=0) does not generate an error (sqlite3_open_v2 returns SQLITE_OK) but a ghost zero byte size file is created (name "file", no extension), an in-memory database is created and the database persists in memory as long as the ghost file exists, even after closing the connection and exiting the process.

parameters for sqlite3_open_v2: sqlite3_open_v2("file:stk.db?mode=memory&cache=shared", &m_db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_FULLMUTEX|SQLITE_OPEN_CREATE, nullptr)

Everything works as expected with SQLITE_USE_URI=1

Versions: at least from 3.34 to 3.37

I am using this specific mode in order to mount in memory a disk database at start-up (with VACUUM INTO or the online backup APIs)

(2) By Keith Medcalf (kmedcalf) on 2022-01-24 16:51:02 in reply to 1 [link] [source]

The result obtained depends on how the Operating System deals with a filename of "file:stk.db?mode=memory&cache=shared".

Microsoft Windows NTFS will treat that as a reference to a datastream called "stk.db?mode=memory&cache=shared" of the file "file" in the current directory.

Your mileage will vary for other filesystems and operating systems according to their documented file naming behaviour.

In order to have Sqlite3 subparse the filename field as a URI one must have enabled Sqlite3 to do so. SQLITE_USE_URI=1 is one way to do so.

(3) By francois (fcsqlite) on 2022-01-28 13:35:57 in reply to 2 [link] [source]

Clear but the result (creating a zero byte file and keeping a database in memory even after closing the connection and the application) remains abnormal and preventable

(4) By Larry Brasfield (larrybr) on 2022-01-28 14:19:07 in reply to 3 [source]

You have misunderstood Keith's answer and elucidation. It is not true that there was a result correctly describable as "creating a zero byte file". You asked for a file, by name, and got one. And its content was where you specified, in the alternate data stream named "stk.db". The file you created, named "file", had two data streams -- an empty one and one not empty.

You can see your two streams with either: (from cmd.exe) dir /R file or (from Powershell) Get-Item -Path file -Stream * .