java数据库损坏,Android的SQLite数据库被损坏

There are about 300 people using my Android App right now and every once and while I get a crash report to the server with this stack trace:

android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)

at android.app.ActivityThread.access$2200(ActivityThread.java:126)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:123)

at android.app.ActivityThread.main(ActivityThread.java:4595)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:521)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed

at android.database.sqlite.SQLiteQuery.native_fill_window(Native Method)

at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:75)

at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:295)

at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:276)

at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:171)

at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)

The result is the app crashing and all the data in the DB being lost.

One thing to note is that every time I read or write to the database I get a new SQLiteDatabase and close it as soon as I'm done. I did this in an attempt to prevent these kind of corruption errors.

I also tried synchronizing all DB reads and writes using a single static object and that didn't seem to help.

Is it possible this is just a SQLite bug?

I found a similar bug with the built-in email app here: http://code.google.com/p/android/issues/detail?id=5610.

Here is my code:

public class KeyValueTableAdapter extends BaseTableAdapter {

private String tableName;

private String keyColumnName;

private String valueColumnName;

public KeyValueTableAdapter(Context context, String tableName, String keyColumnName, String valueColumnName) {

super(context);

this.tableName = tableName;

this.keyColumnName = keyColumnName;

this.valueColumnName = valueColumnName;

}

protected String getStringValue(int key) {

Cursor cursor = null;

SQLiteDatabase db = null;

String value;

try {

db = dbOpenHelper.getReadableDatabase();

cursor = db.query(true, tableName, new String[] { valueColumnName }, keyColumnName + "=" + key, null, null, null, null, null);

if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {

value = null;

} else {

value = cursor.getString(0);

}

} finally {

if (cursor != null) cursor.close();

if (db != null) db.close();

dbOpenHelper.close();

}

return value;

}

}

public abstract class BaseTableAdapter {

protected DbOpenHelper dbOpenHelper;

public BaseTableAdapter(Context context) {

this.dbOpenHelper = new DbOpenHelper(context, DatabaseSettings.DATABASE_NAME, null, DatabaseSettings.DATABASE_VERSION);

}

}

解决方案

"the DB holds session information so

it's not very feasible to do a backup.

The data changes by the minute"

You should try using SharedPreferences: it stores key-value pairs (in the background, it uses a file).

Storing values:

SharedPreferences sp=MyActivity.getSharedPreferences("Name", Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sp.edit();

editor.putString("key", value);

editor.putBoolean("another", true);

editor.commit();

Retrieving data:

sp.getString("key", "Not found");

// "Not found" is the default value

// if sp does not contain the specified key

sp.getBoolean("another", false);

// false is the default value

// if sp does not contain the specified key

See getSharedPreferences and SharedPreferences for a more detailed description.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值