Android DatabaseObjectNotClosedException

时间:2015-10-09 06:56:58

标签: android

我在更新查询时遇到SQLite数据库的错误,我不知道DatabaseObjectNotClosedException的位置:应用程序没有关闭此处打开的游标或数据库对象。这是日志信息

10-09 12:19:03.116  15617-15628/? E/SQLiteDatabase﹕ close() was never explicitly called on database '/data/data/com.example.tazeen.classnkk/databases/ClassNKK'
    android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
            at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
            at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
            at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
            at com.example.DBsqlite.MyDbHelper.update_AllPost(MyDbHelper.java:915)
            at com.example.tazeen.classnkk.AllPosts_Page.getAllActivityList(AllPosts_Page.java:1473)
            at com.example.tazeen.classnkk.AllPosts_Page$GetAllActivityList.doInBackground(AllPosts_Page.java:1355)
            at com.example.tazeen.classnkk.AllPosts_Page$GetAllActivityList.doInBackground(AllPosts_Page.java:1344)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
10-09 12:19:03.116  15617-15628/? E/System﹕ Uncaught exception thrown by finalizer
10-09 12:19:03.125  15617-15628/? E/System﹕ java.lang.IllegalStateException: Don't have database lock!
            at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
            at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
            at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
            at android.util.LruCache.trimToSize(LruCache.java:197)
            at android.util.LruCache.evictAll(LruCache.java:285)
            at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
            at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
            at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
            at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
            at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
            at java.lang.Thread.run(Thread.java:856)

这是我在MyDbHelper中的方法

public boolean update_AllPost(String strActivityId,
                                  String strRemark,
                                  String strName,
                                  String strInspectorName,
                                  String strShortname,
                                  String strHashTag,
                                  String strStatus,
                                  String strType,
                                  String strType_RGN,
                                  String strType_ShortRGN,
                                  String strUserId,
                                  String strObjectId,
                                  String strActiondate,
                                  String strStarFlag,
                                  String strDownLoadStatus)
    {
        SQLiteDatabase db1 = getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(COL_AllPost_PostActivityId, strActivityId);
        args.put(COL_AllPost_Remark, strRemark);
        args.put(COL_AllPost_Title, strName);
        args.put(COL_AllPost_inspectorname , strInspectorName);
        args.put(COL_AllPost_shortName ,strShortname);
        args.put(COL_AllPost_HashTag ,strHashTag);
        args.put(COL_AllPost_Status, strStatus);
        args.put(COL_AllPost_Type, strType);
        args.put(COL_AllPost_TypeRGN,strType_RGN);
        args.put(COL_AllPost_TypeShortRGN, strType_ShortRGN);
        args.put(COL_AllPost_UserId, strUserId);
        args.put(COL_AllPost_VesselId, strObjectId);
        args.put(COL_AllPost_ActionDate, strActiondate);
        args.put(COL_AllPost_StarFlag, strStarFlag);
        args.put(COL_AllPost_DownLoadStatus , strDownLoadStatus);

        int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "=" + strActivityId, null);
        return i > 0;

}

2 个答案:

答案 0 :(得分:2)

更新后使用db1.close()。

这是一个适合我的例子:

public int updateWork(Work work) {

// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();

// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("DATE", work.getDate(false));
values.put("STARTTIME", work.getStartTime());   // get startTime
values.put("ENDTIME", work.getEndTime());    // get getEndtime

// 3. updating row
//db.update(table,column/value,selections,selection)
int i = db.update(TABLE_WORK, values, DATE + " = ?", new String[] { String.valueOf(work.getDate(false)) });

// 4. close
db.close();

return i;

}

答案 1 :(得分:0)

一旦你打开数据库就应该关闭它,希望这会对你有帮助

  db1.close();
 return i;