title: Attempt to write a readonly database... but I'm not
body: <p>I have a read-only database connection. Sometimes, when reading data from the database with a <code>SELECT</code> query, it throws a <code>SQLiteReadOnlyDatabaseException</code>.</p>
  
  <p>I open the connection like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>return SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
  </code></pre>
  
  <p>The query is:</p>
  
  <pre><code>Select * FROM BudgetVersions WHERE entityId = ?
  </code></pre>
  
  <p>I read data from the database using <code>db.rawQuery()</code>, like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>String query = ...;
  Cursor c = db.rawQuery(query, new String[]{ activeBudgetId });
  try {
      if (c.moveToFirst()) {            
          bv.versionName = c.getString(c.getColumnIndexOrThrow("versionName"));
          return bv;
      } else {
          return null;
      }
  } finally {
      c.close();
  }
  </code></pre>
  
  <p>Very rarely, I get a crash like this, inside the call to <code>c.moveToFirst()</code>: </p>
  
  <pre class="lang-none prettyprint-override"><code>Caused by: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 776)
  at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
  at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845)
  at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
  at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
  at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
  at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
  at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
  at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
  </code></pre>
  
  <p>As a workaround, I could try using a writable database connection instead, but I'd like to know why the crash is happening.</p>
  
  <p>The table I'm reading from is a standard SQLite table:</p>
  
  <pre><code>CREATE TABLE BudgetVersions (
      entityId        VARCHAR  PRIMARY KEY NOT NULL UNIQUE,
      budgetId        VARCHAR  NOT NULL,
      versionName     VARCHAR  NOT NULL,
      dateFormat      VARCHAR,
      currencyFormat  VARCHAR,
      lastAccessedOn  DATETIME,
      isTombstone     BOOL     NOT NULL,
      deviceKnowledge NUMERIC  NOT NULL
  );
  </code></pre>
  
  <p>I've seen the crash happen on both a KitKat emulator and a device running Lollipop.</p>
  
  <hr>
  
  <p>There is a <strong>separate</strong> writeable connection open to the same database <strong>at the same time</strong>, owned by a WebView. The database is being updated by Javascript code in the WebView, and read from in the native Android/Java layer with this read-only connection.</p>
  
  <p>I expect this may prove to be the ultimate cause of the problem, but I'd like to understand in detail why a read-only connection would interfere with a separate writeable connection.</p>
  
  <p>I am well aware that the general advice is to use a single connection to the database, but since the writeable connection is owned by the <code>WebView</code>, I don't have easy access to it from the Java code.</p>