SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)

前端 未结 5 926
暗喜
暗喜 2020-12-31 07:59

So in some rare instances, I\'m seeing the \"attempt to write a readonly database\" message, and I can\'t figure out where the problem lies. I\'ll start with the stacktrace

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 08:44

    I'm having a similar issue, which is really annoying is that it happends at any time, it's difficult to replicate the exact conditions that make it happen. I only close the DDBB in the ondestroy method of the MainActivity class. What I have done is to add a try / catch in every use of the db and add the following catch, in this case it is in the middle of a while loop, in other functions I call the function again once:

    catch (SQLException e) {
        e.printStackTrace();
        Log.d(TAG, "MainService: error in AccSaveToDB with "+mainDB.getPath()+" in iteration "+j+". Closing and re-opening DB");
        DBHelper.close();
        mainDB.close();
        j--;
    }
    

    And this at the begining of each function that access to the database:

    if (mainDB==null || !mainDB.isOpen()) {
        DBHelper = DefSQLiteHelper.getInstance(getApplicationContext(), "Data.db", null, 1);
        mainDB = DBHelper.getWritableDatabase();
    }
    

    So far I still have sometime this error, I couldn't figure out the cause yet, but at least my app doesn't crash and it resumes what it has to do. I can't see if the file is being deleted, but this solutions is working for me

提交回复
热议问题