【问题标题】:Database problem: how to diagnose and fix the problem?数据库问题:如何诊断和解决问题?
【发布时间】:2010-12-16 07:18:55
【问题描述】:

我创建了一个应用程序,它将值存储到数据库中并检索存储的数据。在运行模式下运行应用程序时,一切似乎都工作正常(值已成功存储和检索),但当我在调试模式下运行时,进程抛出 IllegalStateException,到目前为止还没有找到原因。

获取对象Recording的方法如下:

public Recording getRecording(String filename) {

    Recording recording = null;
    String where = RECORDING_FILENAME + "='" + filename + "'";
    Log.v(TAG, "retrieving recording: filename = " + filename);

    try {
        cursor = db.query(DATABASE_TABLE_RECORDINGS, new String[]{RECORDING_FILENAME, RECORDING_TITLE, RECORDING_TAGS, RECORDING_PRIVACY_LEVEL, RECORDING_LOCATION, RECORDING_GEO_TAGS, RECORDING_GEO_TAGGING_ENABLED, RECORDING_TIME_SECONDS, RECORDING_SELECTED_COMMUNITY}, where, null, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            //String filename = c.getString(0);
            String title = cursor.getString(1);
            String tags = cursor.getString(2);
            int privacyLevel = cursor.getInt(3);
            String location = cursor.getString(4);
            String geoTags = cursor.getString(5);
            int iGeoTaggingEnabled = cursor.getInt(6);
            String recordingTime = cursor.getString(7);
            String communityID = cursor.getString(8);
            cursor.close();
            recording = new Recording(filename, title, tags, privacyLevel, location, geoTags, iGeoTaggingEnabled, recordingTime, communityID);
        }
    }
    catch (SQLException e) {
        String msg = e.getMessage();
        Log.w(TAG, msg);
        recording = null;
    }
    return recording;
}

它是从另一个类(设置)中调用的:

private Recording getRecording(String filename) {
    dbAdapter = dbAdapter.open();
    Recording recording = dbAdapter.getRecording(filename);
    dbAdapter.close();
    return recording;
}

运行上面的代码时一切正常,但随后我注意到另一个线程中出现异常: alt text http://img509.imageshack.us/img509/862/illegalstateexception.jpg

既不知道此异常的可能原因是什么,也不知道如何从该线程调试代码以诊断原因。

如果有人知道这里可能出现的问题,我将非常感激。

谢谢!

【问题讨论】:

  • 异常的完整堆栈跟踪是什么?或者至少是相关位?

标签: database android exception


【解决方案1】:

看起来cursor.close() 在“if”内——那时SQLiteCursor.finalize() 会抛出一个 IllegalStateException(我用谷歌搜索过)。例如,如果其他进程/线程没有时间提交,您可能会得到一个空记录集。

总是关闭它,即使它的结果集是空的。

我还建议您按名称访问字段,而不是索引,以便将来兼容。并在finally{} 块中同时执行close()s。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多