Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've just started developing for Android, and was surprised to discover that bundling a static database is <em>not easy to do</em>. So I did the only reasonable thing: created a library which does just that. </p> <p><a href="https://bitbucket.org/kiwidrew/android-staticdb" rel="noreferrer">android-staticdb</a> is an Android library to allow read-only access to SQLite databases contained in an application's <code>.apk</code> file. This is ideal for apps which need to ship with a large quantity of static data (e.g. an offline Wikipedia reader).</p> <p>It works by registering a new "virtual filesystem" layer with the SQLite library from native C code, which is able to intercept the filesystem calls made by SQLite. Because there is only one copy of the SQLite library per VM process, we actually end up intercepting any SQLite calls made by <em>any</em> application in the same process as us!</p> <p>In normal operation, our VFS layer simply proxies all calls to the "default" VFS, which just uses open() and read() to access regular database files. But when a special filename matching the <code>*.apk!filename</code> pattern is encountered, our VFS grabs control. Using zlib and minizip, it opens the <code>.apk</code> file and looks inside for the database file; it will then read chunks of this file to satisfy any <code>read()</code> requests from SQLite.</p> <p>By doing this in this manner, applications can continue to use the standard Android database APIs.</p> <p>Example usage:</p> <pre><code>import android.database.sqlite.SQLiteDatabase; import kiwidrew.staticdb.StaticDatabase; public class BlahBlah extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SQLiteDatabase db = StaticDatabase.openDatabase(this, "assets/foobar.db"); // Do normal database stuff with 'db'... } } </code></pre> <p>You get back a standard SQLiteDatabase object, with the only restriction being that it doesn't support writing. (Obviously!)</p> <p>Note that this will fail unless the database is stored in your <code>.apk</code> without compression. Add the SQLite database using the <code>aapt -0</code> command or modify your <code>build.xml</code> to pass the <code>&lt;nocompress extension="db" /&gt;</code> flag to the <code>&lt;aapt&gt;</code> tag...</p> <h3>Note:</h3> <p>I've literally just finished writing this, and have only done very basic testing so far. Bug reports would be appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload