日期:2014-05-16  浏览次数:20552 次

SQLite抛出错误“未关闭游标及数据库对象”特殊原因一例
Android 2.3.3
Eclipse Version: 3.7.0
LogCat

?LogCat 报错信息:

02-13 13:29:32.830: WARN/ActivityManager(62): Launch timeout has expired, giving up wake lock!  
02-13 13:29:33.381: DEBUG/dalvikvm(17173): GC_CONCURRENT freed 771K, 57% free 2908K/6727K, external 2013K/2137K, paused 7ms+5ms  
02-13 13:29:33.630: DEBUG/dalvikvm(17173): GC_FOR_MALLOC freed 867K, 58% free 2836K/6727K, external 2013K/2137K, paused 178ms  
02-13 13:29:33.859: ERROR/Database(17173): close() was never explicitly called on database '/data/data/com.tmall.nokia/databases/taobaoNokia.db' 
02-13 13:29:33.859: ERROR/Database(17173): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-13 13:29:33.859: ERROR/Database(17173): at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1847)
02-13 13:29:33.859: ERROR/Database(17173): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
02-13 13:29:33.859: ERROR/Database(17173): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
02-13 13:29:33.859: ERROR/Database(17173): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
02-13 13:29:33.859: ERROR/Database(17173): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
02-13 13:29:33.859: ERROR/Database(17173): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-13 13:29:33.859: ERROR/Database(17173): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
02-13 13:29:33.859: ERROR/Database(17173): at com.tmall.nokia.db.RecordDBHelper.query(RecordDBHelper.java:49)
02-13 13:29:33.859: ERROR/Database(17173): at com.tmall.nokia.manage.RecordDBopt.get(RecordDBopt.java:78)
02-13 13:29:33.859: ERROR/Database(17173): at com.tmall.nokia.manage.RecordMessage.sendMseeage(RecordMessage.java:111)
02-13 13:29:33.859: ERROR/Database(17173): at com.tmall.nokia.manage.RecordMessage.sendMseeage(RecordMessage.java:87)
02-13 13:29:33.859: ERROR/Database(17173): at com.tmall.nokia.Report.run(Report.java:49)
02-13 13:29:33.859: ERROR/Database(17173): at java.lang.Thread.run(Thread.java:1019)
02-13 13:29:33.830: WARN/ActivityManager(62): Launch timeout has expired, giving up wake lock!
02-13 13:29:34.381: DEBUG/dalvikvm(17173): GC_CONCURRENT freed 771K, 57% free 2908K/6727K, external 2013K/2137K, paused 7ms+5ms
02-13 13:29:34.630: DEBUG/dalvikvm(17173): GC_FOR_MALLOC freed 867K, 58% free 2836K/6727K, external 2013K/2137K, paused 178ms

?

发生错误原因分析:

主要错误:

02-13 13:29:33.859: ERROR/Database(17173): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

?

出错处代码

		SQLiteDatabase db = getWritableDatabase();

?

意思是指在此处打开数据库操作之前,应用程序没有关闭游标或数据库对象。

在应用中,对数据库操作完成后,应该对游标及数据库进行关闭操作

		if (!cur.isClosed()) {
			cur.close();
		}
		db.close(); 

?

检查操作数据库的相关源代码,确认在退出前关闭了游标及数据库对象。
那还抛出未关闭的错误是怎么回事呢?继续检查发现,抛出此错误之前,在操作数据库过程中还抛出了另外的一个错误。

原来在抛出第一个错误以后,关闭操作未执行,导致再次操作数据库时抛出“应用程序没有关闭游标或数据库对象”的错误。

?

解决办法:

检查数据库操作部分代码,在抛出错误处理部分,关闭游标及数据库对象(cursor or database object)