Attempt to write a readonly database… but I'm not

前端 未结 1 1372
长发绾君心
长发绾君心 2021-01-07 16:38

I have a read-only database connection. Sometimes, when reading data from the database with a SELECT query, it throws a SQLiteReadOnlyDatabaseException

1条回答
  •  伪装坚强ぢ
    2021-01-07 17:20

    Solved by changing it to a writeable database connection. The clue was in the documentation for the 776 error code:

    (776) SQLITE_READONLY_ROLLBACK

    The SQLITE_READONLY_ROLLBACK error code is an extended error code for SQLITE_READONLY. The SQLITE_READONLY_ROLLBACK error code indicates that a database cannot be opened because it has a hot journal that needs to be rolled back but cannot because the database is readonly.

    During development, I am frequently interrupting the currently-running app to install and run a new version. This causes the currently-running app to be force-stopped by the system. If the Javascript code in the WebView is in the middle of writing to the database via its separate writeable connection when the app is nuked, then a hot journal will be left behind.

    When the new version of the app starts up, the read-only database connection in the native Java code is opened. When this connection spots the journal, it tries to roll back the journal. And because it's a read-only connection, it fails.

    (This fits with the crash being observed immediately on startup after I've made a change.)

    The correct fix is therefore to make the Java connection a writeable connection. This connection never attempts a write during normal operation, but it must write when recovering from a previous interrupted write through the WebView's writeable connection.

    0 讨论(0)
提交回复
热议问题