当前位置:首页 > 数据库 > SQlite

android-SQLiteDiskIOException:磁盘I / O错误(代码1802):,而在编译时:PRAGMA journal_mode

我最近遇到了一些崩溃,最近崩溃的频率越来越高,我无法弄清楚如何重现或修复它们.这是最常出现的那个

Fatal Exception: java.lang.RuntimeException: Unable to create application com.myapp.MyApplication: android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 1802): , while compiling: PRAGMA journal_mode
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5436)
   at android.app.ActivityThread.-wrap2(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6154)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 1802): , while compiling: PRAGMA journal_mode
   at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
   at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:634)
   at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:320)
   at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:294)
   at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
   at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
   at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
   at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
   at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
   at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
   at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:680)
   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:289)
   at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
   at com.myapp.DBConnect.getInstance(DBConnect.java:202)
   at com.myapp.MyApplication.onCreate(MyApplication.java:66)

我尝试进行的每次搜索都听起来像是问题在于尝试从不同的线程打开数据库.但是,我认为我可以通过在helper类中使用静态方法来获取当前实例来避免这种情况.

private static DBConnect mInstance = null;
private static SQLiteDatabase db;

public static DBConnect getInstance(Context ctx) {
    if (mInstance == null) {
        mInstance = new DBConnect(ctx.getApplicationContext());
        db = mInstance.getWritableDatabase(); // this is line 202 in DBConnect.java
        // all methods in this class reference this db var to run database queries
    }
    return mInstance;
}

然后在需要访问数据库的任何地方,我都可以使用此行设置本地变量db:

// this is line 66 in MyApplication.java
DBConnect db = DBConnect.getInstance(this);

然后使用db.addItem()之类的函数运行我所有的数据库查询

它一直运行良好,我90%确信多个线程正在访问相同的表,而我从未见过崩溃.该应用程序有几个列表,这些列表在后台在多个位置同步,最容易重现的是应用程序首次启动时执行这些列表的同步.同步时,用户可以查看不同的列表,正在加载每个列表是从数据库中选择其内容,可能是在后台同步过程中从同一表中添加或删除项目时.我每天也有数百名活跃用户,并且在过去几个月中仅出现过15次此类崩溃.

另外,看到错误的第一行是java.lang.RuntimeException:无法创建应用程序…,似乎是在用户首次启动应用程序时发生的?不确定此时是否已经有多个线程,除非用户登录,否则不会运行任何后台进程.

我还看到由同一行引起的以下错误:

Failed to change locale for db '/data/user/0/com.myapp/databases/mydb' to 'en_US'.

我尝试更改手机上的语言环境设置,重新安装应用程序,然后四处乱逛,看看是否遇到此问题.在使用其他语言环境进行安装后,我什至尝试切换回英语,但无法再次发生此崩溃.我觉得它们可能相关,因为它们似乎都在启动时发生.

解决方法:

在我的情况下,我没有授予运行时权限,我将数据库文件保存在外部存储中,当要求获得许可时,我将其保存为Denny,但是在“允许”许可后,它可以正常工作.在高于23的sdk版本上,您必须授予运行时权限.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!