Can I use H2, HSQLDB, or any other embed开发者_运维问答ded database, with a database from an InputStream
instead of a file?
I'm planning to use AssetManager.open()
on Android, which can return an InputStream
in random access mode.
H2 supports a pluggable file system that allows you to access read-only databases in a zip or jar files. However, there is currently no file system implementation for the AssetManager
. It should be relatively easy to implement it. The best starting point is probably FileSystemZip and FileObjectZip.
Most databases need random access to underlying files so an InputStream will not do.
AFAIK, H2 and HSQLDB provide only file-based and in-memory connection URIs when used in "standalone" mode. So a file, or some heap memory space, shall be needed; but you don't give it a File/InputStream, everything is in the connection URI, eg jdbc:h2:~/test
.
If you're prepared to do a bit of development, it can be done with HSQLDB, especially version 1.8.1.x which is smaller in size.
Check the org.hsqldb.lib.ResourceStreamProvider class and modify it to use the streams that you provide for file names db.properties and db.script when the calls are made to its static getResourceAsStream method.
Pretty simple, especially because the db.script and db.properties consist of text, which you can easily generate in the rest of your program.
精彩评论