Android - database disk image is malformed

前端 未结 3 644
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 21:10

In my android app I am getting \"database disk image is malformed\" What are the reasons for getting this error?

not closed db? multiple threads accessing the db? or

相关标签:
3条回答
  • 2020-12-15 21:18

    I've come across many instances of people reporting sqlite corruption issues on Android. The majority of times it's related to a task killer but there is still a small margin of people still experiencing random SQLite corruption.

    For example, this issue: http://code.google.com/p/android/issues/detail?id=4866

    JRL's links above are also very useful for understanding what types of events can lead to a corrupted SQLite DB image.

    Also, SQLite itself has been patched in recent history to resolve various (rare) data corruption scenarios. See http://www.sqlite.org/changes.html. So the version of SQLite that ships with Android isn't the latest, but as Android evolves, so do the apps that are bundled with it, such as SQLite.

    At the end of the day, there's only so much we can do as programmers to safeguard against SQLite corruption so its sometimes advantageous to code safety mechanisms into our apps, such as periodic DB backups to SDCard (that's what I do).

    0 讨论(0)
  • 2020-12-15 21:23

    Fixed in Version 3.5.4 (2007-12-14): Any DELETE or UPDATE operation that uses side-effects to delete additional rows of the same table that is the subject of the DELETE or UPDATE might cause database corruption. The issue was first identified by ticket #2832. But the issue is very old and effects all versions of SQLite at least back through version 3.3.13 (the earliest version that we have checked.)

    A "delete side-effect" in the previous paragraph means a deletion that occurs as a result of an OR REPLACE clause or due to a trigger. For example:

       CREATE TABLE ex1(a INTEGER PRIMARY KEY, b);
       INSERT INTO ex1 VALUES(1,2);
       INSERT INTO ex1 VALUES(2,3);
       CREATE TRIGGER ex1_tr1 AFTER UPDATE ON ex1 BEGIN
         DELETE FROM ex1 WHERE a=old.b;
       END;
       UPDATE ex1 SET b=b+1;
    

    In the example above, the first cycle of the UPDATE causes the trigger to fire and delete the second row of the ex1 table. When the second cycle of the UPDATE loop runs, it attempts to process the second row of the ex1 table. SQLite recognized that the second row had been deleted so it aborts the second cycle, but it was failing to clean up after itself properly which could lead to database corruption on subsequent cycles of the loop.

    0 讨论(0)
  • 2020-12-15 21:42

    The error is passed on from the native code to Java, so you have to look at possible causes of SQLite corruption. Here's a webpage on the SQLite website that lists the ones due to bugs in SQLite, and here's another titled How to corrupt your database.

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