Android - 数据库磁盘映像格式错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2960015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - database disk image is malformed
提问by Daniel Benedykt
In my android app I am getting "database disk image is malformed" What are the reasons for getting this error?
在我的 android 应用程序中,我收到“数据库磁盘映像格式错误”出现此错误的原因是什么?
not closed db? multiple threads accessing the db? or android just got corrupted?
没有关闭数据库?多个线程访问数据库?或者 android 刚刚损坏?
Thanks
谢谢
android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
android.database.sqlite.SQLiteQuery.native_fill_window(Native Method)
android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:75)
android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:288)
android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:269)
android.database.AbstractCursor.moveToPosition(AbstractCursor.java:171)
android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)
...
回答by Brad Hein
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.
我遇到过很多人在 Android 上报告 sqlite 损坏问题的实例。大多数情况下,它与任务杀手有关,但仍有一小部分人仍然遇到随机 SQLite 损坏。
For example, this issue: http://code.google.com/p/android/issues/detail?id=4866
例如这个问题: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.
上面 JRL 的链接对于了解哪些类型的事件会导致 SQLite 数据库映像损坏也非常有用。
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.
此外,SQLite 本身已在最近的历史中进行了修补,以解决各种(罕见的)数据损坏情况。请参阅http://www.sqlite.org/changes.html。因此,Android 附带的 SQLite 版本不是最新的,但随着 Android 的发展,与其捆绑的应用程序(例如 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).
归根结底,我们作为程序员只能做很多事情来防止 SQLite 损坏,因此有时将安全机制编码到我们的应用程序中是有利的,例如定期将 DB 备份到 SDCard(这就是我所做的)。
回答by AZ_
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.)
版本 3.5.4 (2007-12-14) 中已修复:任何使用副作用删除作为 DELETE 或 UPDATE 主题的同一表的其他行的 DELETE 或 UPDATE 操作都可能导致数据库损坏。该问题首先由工单 #2832 确定。但是这个问题已经很老了,至少会影响 SQLite 的所有版本,至少可以追溯到 3.3.13 版(我们检查过的最早版本)。
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:
上一段中的“删除副作用”是指由于 OR REPLACE 子句或由于触发器而发生的删除。例如:
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 corruptionon subsequent cycles of the loop.
在上面的示例中,UPDATE 的第一个循环导致触发器触发并删除 ex1 表的第二行。当 UPDATE 循环的第二个循环运行时,它尝试处理 ex1 表的第二行。SQLite 识别出第二行已被删除,因此它中止了第二个循环,但它无法正确地自行清理,这可能导致循环的后续循环中的数据库损坏。
回答by JRL
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 webpageon the SQLite website that lists the ones due to bugs in SQLite, and here's another titled How to corrupt your database.
错误从本机代码传递到 Java,因此您必须查看 SQLite 损坏的可能原因。这是SQLite 网站上的一个网页,其中列出了由于 SQLite 中的错误而导致的网页,还有另一个标题为“如何损坏您的数据库”的网页。