Android sqlite日志报告和db.close()

时间:2011-01-11 17:10:46

标签: java android sqlite

日志报告数据库或游标未关闭。我基本上有一个带有自定义surfaceview的活动,并使用处理程序报告回活动。当我收到消息时,我会显示一个alertdialog并更新数据库。

private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        switch(msg.what) {
        case 1:
            dh.open();
            dh.updateEvent("id", "name", "someone");
            dh.close();
            successAlert.show();
            break;
        case 2:
            failAlert.show();
            break;
        }
    }
};

以前我没有“dh.close()”,那就是当日志报告数据库/游标未关闭时。但是自从我加入之后,它需要很长时间才能完成。收到消息后,系统似乎挂起了。我做错了什么或通常需要这么长时间。我也试过使用try块,最后关闭数据库。

编辑:

public class DatabaseHelper {

private Database dbHelper;
private SQLiteDatabase db;
private Context context;

public DatabaseHelper(Context context) {
    this.context = context;
    //database = new Database(context);
}

public void open() {
    dbHelper = new Database(context);
    db = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public void updateEvent(int id, String name, int other) {
    ContentValues cv = new ContentValues();
    cv.put("id", id);
    cv.put("name", name);
    cv.put("other", other);
    db.update("stateTable", cv, "id=" + id, null);
}

public boolean checkState(int id) {
    db = dbHelper.getReadableDatabase();
    Cursor cursor = db.query("stateTable", null, null, null, null, null, null);
    cursor.moveToPosition(id - 1);
    int i = cursor.getInt(2);
    android.util.Log.d("semajhan", ": " + i);
    if (i == 1) {
        return true;
    } else {
        return false;
    }
}

}

扩展的SQLiteOpenHelper:

public class Database extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "events.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "stateTable";
private static final String ID = "id";
private static final String NAME = "name";
private static final String OTHER = "other";
private static final String DATABASE_CREATE = "CREATE TABLE stateTable (id INT, name TEXT, other INT)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS table";

public Database(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(DATABASE_CREATE);
    // added initial values
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL(DATABASE_UPGRADE);
    onCreate(db);
}

}

01-11 13:57:41.239: ERROR/ActivityManager(61): ANR in com.semajhan.soodles  (com.semajhan.soodles/.Level1)
01-11 13:57:41.239: ERROR/ActivityManager(61): Reason: keyDispatchingTimedOut
01-11 13:57:41.239: ERROR/ActivityManager(61): Load: 1.64 / 0.56 / 0.26
01-11 13:57:41.239: ERROR/ActivityManager(61): CPU usage from 35716ms to -1ms ago:
01-11 13:57:41.239: ERROR/ActivityManager(61):   44% 862/com.semajhan.soodles: 37% user + 7.2% kernel / faults: 853 minor
01-11 13:57:41.239: ERROR/ActivityManager(61):   29% 61/system_server: 27% user + 1.9% kernel / faults: 142 minor
01-11 13:57:41.239: ERROR/ActivityManager(61):   0.2% 731/com.android.quicksearchbox: 0% user + 0.2% kernel / faults: 30 minor
01-11 13:57:41.239: ERROR/ActivityManager(61):   0.2% 707/com.android.launcher: 0.2% user + 0% kernel / faults: 30 minor
01-11 13:57:41.239: ERROR/ActivityManager(61):   0.2% 801/com.svox.pico: 0.1% user + 0.1% kernel / faults: 363 minor
01-11 13:57:41.239: ERROR/ActivityManager(61):   0% 117/com.android.systemui: 0% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):   0% 41/adbd: 0% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 99% TOTAL: 86% user + 13% kernel + 0% irq
01-11 13:57:41.239: ERROR/ActivityManager(61): CPU usage from 1969ms to 2620ms later:
01-11 13:57:41.239: ERROR/ActivityManager(61):   54% 61/system_server: 48% user + 6% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):     40% 69/SurfaceFlinger: 40% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):     10% 92/InputDispatcher: 7.5% user + 3% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):     1.5% 62/HeapWorker: 1.5% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):   44% 862/com.semajhan.soodles: 32% user + 12% kernel / faults: 2 minor
01-11 13:57:41.239: ERROR/ActivityManager(61):     24% 874/Thread-13: 24% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):     23% 862/studios.soodles: 4.6% user + 18% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):     1.5% 867/Compiler: 0% user + 1.5% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):   0.8% 731/com.android.quicksearchbox: 0% user + 0.8% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61):     0.8% 732/HeapWorker: 0% user + 0.8% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 100% TOTAL: 76% user + 23% kernel

3 个答案:

答案 0 :(得分:5)

第一次调用getReadableDatabase实例的getWritableDatabaseSQLiteOpenHelper需要真的很长时间才能完成。每次需要查询数据库时,都不应创建新的Database对象(SQLiteOpenHelper实例)。尝试在Database中使用相同的DatabaseHelper实例。

使用SQLiteOpenHelper时,您不希望关闭SQLiteDatabase的{​​{1}}对象,因为它是共享的;即SQLiteOpenHelper始终返回相同的getWritableDatabase对象。

请注意,您的SQLiteDatabase方法会泄漏游标。为了帮助防止游标泄漏,我总是在获得游标后使用checkState - try。例如:

finally

答案 1 :(得分:3)

我想知道关于光标未被关闭的基本问题是否与this answer中的相同:活动需要有onDestroy方法来关闭DatabaseHelper。 (我现在可以告诉你的是,你没有显示一个onDestroy方法,但这显然只是你代码的一部分。)我还在博客上写了这个问题{{3 }}

你还写道:“但是自从我添加[dh.close()]以后,它需要很长时间才能完成。”您是否正在对数据库进行大量写入操作?也许您需要使用事务来定期刷新这些写入。如果您只是从数据库中读取,那么我不知道为什么close调用需要很长时间才能完成。但是,如果没有看到更多的代码,这些只是猜测。

编辑:here关于在致电db.close()之前致电dbHelper.getReadableDatabase()的建议值得关注。但是,您需要检查db是否为null。这会将checkState更改为:

public boolean checkState(int id) {
    if (db != null) {
        db.close();
    }
    db = dbHelper.getReadableDatabase();
    // ... etc ....

如果事实证明这是真正的问题,请接受ccheneson的答案,而不是我的答案。

(以上编辑不正确;我应该知道的更好!请参阅下文。)

编辑1 :我不确定您为什么要在此方法中重新打开数据库。假设您调用了DatabaseHelper.open方法,那么您有一个可读的可写数据库句柄。 checkState不需要重新打开它来执行读取。

编辑2 :但是,ccheneson几乎总是返回从SQLiteOpenHelper.getWriteableDatabase返回的相同数据库句柄。您需要显式关闭该数据库句柄; SQLiteOpenHelper 将为您关闭它。您执行需要确保在您的活动被销毁时调用SQLiteOpenHelper.close(如上所述。)

答案 2 :(得分:1)

public void close() {
    dbHelper.close(); // to change to db.close(); ?
}

我想在这里你想要关闭数据库句柄db而不是dbHelper

同样在你的checkState方法中:

public boolean checkState(int id) {
    db = dbHelper.getReadableDatabase();
    Cursor cursor = db.query("stateTable", null, null, null, null, null, null);
    cursor.moveToPosition(id - 1);
    int i = cursor.getInt(2);
    android.util.Log.d("semajhan", ": " + i);
    if (i == 1) {
        return true;
    } else {
        return false;
    }
}

您可能希望关闭光标,如:

int i = cursor.getInt(2);
cursor.close();
android.util.Log.d("semajhan", ": " + i);