1. android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evaluation/com.evaluation.VehicleActivity}: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
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:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
at android.database.sqlite.SQLiteDatabase.dbclose(Native Method)
at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:363)
at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)
at android.database.sqlite.SQLiteProgram.onAllReferencesReleased(SQLiteProgram.java:116)
at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)
at android.database.sqlite.SQLiteProgram.close(SQLiteProgram.java:293)
at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:133)
at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:532)
at com.DatabaseHelper.selectAssessInformation(DatabaseHelper.java:338)
at com.JSKApplication.selectAssessInformation(JSKApplication.java:631)
at com.VehicleActivity.onCreate(VehicleActivity.java:548)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
... 11 more
這個錯誤翻譯過來就是:不能關閉數據庫由於未完成的語句。
最可能的原因是:你在操作sqlite數據庫時使用多線程了,但sqlite數據庫是不支持多線程操作,所以你必須實現多線程同步的機制。
2.android.database.sqlite.SQLiteConstraintException:
error code 19: constraint failed
約束失敗,導致這類錯誤,原因一般有兩個:
1)插入的數據有一個是主鍵,而且插入的主鍵相同;
2)插入的數據有一條數據爲空,而數據庫中定義不能爲空,也會導致這樣的錯誤;
3. close() was never explicitly called on database
10-09 19:24:41.269: E/Database(1269): close() was never explicitly called on database '/data/data/com.android.providers.media/databases/external-47cd50a6.db'
10-09 19:24:41.269: E/Database(1269): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
10-09 19:24:41.269: E/Database(1269): at
對於Android中的SQLite數據庫,我一般採用的方法是,隨用隨取隨關。 進行DAO操作時,首先獲取一個SQLiteDatabase對象,具體代碼採用try{}finally{}的格式。確保有任何問題,數據庫對象都被關閉。