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.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
Andrew
  • 2,663
  • 6
  • 28
  • 50
  • 1
    I don't think the SQLite API supports opening a memory block of some sorts -> http://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. – zapl Aug 24 '12 at 14:40
  • Maybe using a Virtual Filesystem like "jimfs"? – Joel Mar 10 '15 at 22:58
  • Apparently you can do this by writing a VFS module for SQLite. See the pointers to some docuemtation at https://sqlite-users.sqlite.narkive.com/lvdwVRm4/convert-byte-array-to-in-memory-db though I don’t know how much code it would take. – andrewdotn Nov 25 '22 at 18:58

1 Answers1

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;