SQLite User Forum

Sqlite connection to a database file in memory?
Login

Sqlite connection to a database file in memory?

(1) By anonymous on 2022-03-04 06:13:13 [source]

https://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/

I can connect to a sqlite database located in a local file e.g.

jdbc:sqlite:C:/sqlite/db/sample.db

In an environment where there is no local disk, but the sqlite database file is in memory, can I have a connection to the sqlite database file?

Thanks!

(2) By Keith Medcalf (kmedcalf) on 2022-03-04 06:29:57 in reply to 1 [link] [source]

Have you tried jdbc:sqlite::memory: as documented on that very page you quoted (https://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/)?

(3) By anonymous on 2022-03-04 06:37:46 in reply to 2 [link] [source]

Thanks for the reply!

jdbc:sqlite::memory:

is to connect to a brand new database memory. There is an example here to restore the database file to the database in memory:

restore from C:\\__tmp\\thing.sqlite

Unfortunately in my setup, there is no disk. So I can't use this either.

(4) By Larry Brasfield (larrybr) on 2022-03-04 13:32:43 in reply to 3 [link] [source]

I think you will need to explain what a "file" is on your system before anybody can help figure out how to use the SQLite API for your problem. SQLite has been used on limited-resource platforms, but we need to know how limited yours is before suggesting a solution here.

(5) By anonymous on 2022-03-04 16:48:13 in reply to 4 [link] [source]

The file is the SQLite database file. I have it in memory, not on disk.

(6) By Simon Slavin (slavin) on 2022-03-04 17:08:29 in reply to 5 [link] [source]

There seems to be some confusion.

If you wanted to work with strings. You can work with strings inside files on disk. Or you can work with strings in memory. There is no such thing as working with strings in files in memory.

SQLite can keep data in a file on disk. Disks are organised with folders and files. In order to know which file to open, you specify the filename.

SQLite can also keep data in memory. Memory is not organised with folders and files. It's just one big thing. If you want to keep data in memory you tell SQLite to connect to memory. There's no way to specify a filename. That's why Keith specified a connection string which included no filename.

If you make have SQLite data in memory, and remove the last connection to that data, the memory is released and the data disappears.

(8) By anonymous on 2022-03-04 17:13:14 in reply to 6 [link] [source]

Thanks for the reply! It sounds like my use case is not supported.

Thanks again!

(7) By Larry Brasfield (larrybr) on 2022-03-04 17:10:18 in reply to 5 [link] [source]

If "the file" is a "file" in the usual sense of the term's usage among programmer's and computer users, then it will have multiple attributes which make it usable from an instance of the SQLite library running in a process: (1) It will have some kind of name by which other processes can open, read, write, and ultimately close the file; (2) the "file" behaves as a sequence of bytes which is the same across accessing processes, in some timeframe; and (3) a degree of persistence exists for that byte sequence. I cannot tell from what you have (cryptically) written so far whether your "file" is like that. Nor can I tell whether your "diskless" system has any persistent storage scheme exposed through an OS API resembling what most operating systems have to facilitate use of (real) files.

If your platform has fopen(), open(), read(), write(), fread(), fwrite(), close() and fclose(), then you need to use the SQLite API and one of its default file access VFS layers in order to keep your DB in a file.

Your in-memory DB is not in a file. It is in something that is made to resemble a file in limited respects by a VFS. On some platforms, it is possible to share an in-memory DB with other processes. Maybe that is true for your mystery platform. If not, you may need to implement a VFS to accomplish your ends.

Due to lack of information about what you are trying to accomplish and from what starting point, I have no idea how you need to proceed. Neither will anybody else until you are more forthcoming about those facts.

(9) By anonymous on 2022-03-04 17:18:54 in reply to 7 [link] [source]

Appreciate the response! No, my platform doesn't have any of the file operations.

I think I got the answer. Thanks!