4

I need to process data from a SQLite database using some Java code. The problem is that the database is retrieved from an external service and thus it is only available as an InputStream.

I would really like to avoid saving the db to the file system before starting to process the data, but I don't see a way of doing it.

I am currently using SqlJet, as it provides an option to create an in-memory database. However there seems to be no way to turn an InputStream / byte array into a database.

There's nothing forcing me to use SqlJet, it can be easily replaced by any other tool if it provides the needed functionality.

3
  • 1
    I don't think the SQLite API supports opening a memory block of some sorts -> sqlite.org/c3ref/open.html - maybe if you create some in memory filesystem but I'd suggest you just save the stream to a file and use that instead. Commented Aug 24, 2012 at 14:40
  • Maybe using a Virtual Filesystem like "jimfs"? Commented Mar 10, 2015 at 22:58
  • Apparently you can do this by writing a VFS module for SQLite. See the pointers to some docuemtation at sqlite-users.sqlite.narkive.com/lvdwVRm4/… though I don’t know how much code it would take. Commented Nov 25, 2022 at 18:58

1 Answer 1

1

It can't be done as such. Perhaps Java supports some mechanism to create some form of virtual file that is presented locally (to the current process) as some physical file.

I can think of one way to work-around the limitation, but that may not be doable or acceptable within your context. You can change the service to return a dump of the database and load that in the receiver's in-memory database:

Here is a way to generate the dump:

# sqlite3
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo (i text);
sqlite> insert into foo values ('a');
sqlite> insert into foo values ('b');
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE foo (i text);
INSERT INTO foo VALUES('a');
INSERT INTO foo VALUES('b');
COMMIT;
Sign up to request clarification or add additional context in comments.

1 Comment

Great idea, it answers the question. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.