android中操作SQLite常見錯誤

來源:互聯網
上載者:User

最近在改應用的一些bug,想起來可以把常見錯誤整理下,方便自己下次更快速找出錯誤原因,也給遇到同樣問題的同學提供解決思路。我會將錯誤歸類,今天先寫一點,以後再增加,歡迎指正。(為了不透漏一些資訊,log中包名被修改了)


1.  android.database.sqlite.SQLiteException: unable to close due to unfinalised statements


[java]
view plaincopy
  1. FATAL EXCEPTION: main  
  2. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evaluation/com.evaluation.VehicleActivity}: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements  
  3.     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)  
  4.     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)  
  5.     at android.app.ActivityThread.access$2300(ActivityThread.java:135)  
  6.     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)  
  7.     at android.os.Handler.dispatchMessage(Handler.java:99)  
  8.     at android.os.Looper.loop(Looper.java:143)  
  9.     at android.app.ActivityThread.main(ActivityThread.java:4914)  
  10.     at java.lang.reflect.Method.invokeNative(Native Method)  
  11.     at java.lang.reflect.Method.invoke(Method.java:521)  
  12.     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
  13.     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)  
  14.     at dalvik.system.NativeStart.main(Native Method)  
  15. Caused by: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements  
  16.     at android.database.sqlite.SQLiteDatabase.dbclose(Native Method)  
  17.     at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:363)  
  18.     at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)  
  19.     at android.database.sqlite.SQLiteProgram.onAllReferencesReleased(SQLiteProgram.java:116)  
  20.     at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)  
  21.     at android.database.sqlite.SQLiteProgram.close(SQLiteProgram.java:293)  
  22.     at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:133)  
  23.     at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:532)  
  24.     at com.DatabaseHelper.selectAssessInformation(DatabaseHelper.java:338)  
  25.     at com.JSKApplication.selectAssessInformation(JSKApplication.java:631)  
  26.     at com.VehicleActivity.onCreate(VehicleActivity.java:548)  
  27.     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)  
  28.     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)  
  29.     ... 11 more  

這個錯誤翻譯過來就是:不能關閉資料庫由於未完成的語句。

我從網上查了一下,最可能的原因是:你在操作sqlite資料庫時使用多線程了,但sqlite資料庫是不支援多線程操作,所以你必須實現多線程同步的機制。

我的程式出錯是因為:在後台啟動了個service定時向伺服器請求資料,並線上程中將資料插入資料庫。在某個時間段,使用者在向資料庫插入資料後,關閉資料庫,而此時線程正在向資料庫中寫入資料,就會造成上述異常。


2.android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed


[java]
view plaincopy
  1. 07-04 16:18:56.677: W/System.err(29830): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed  

約束失敗,導致這類錯誤,原因一般有兩個:

1)插入的資料有一個是主鍵,而且插入的主鍵相同;

2)插入的資料有一條資料為空白,而資料庫中定義不可為空,也會導致這樣的錯誤;

最近在改應用的一些bug,想起來可以把常見錯誤整理下,方便自己下次更快速找出錯誤原因,也給遇到同樣問題的同學提供解決思路。我會將錯誤歸類,今天先寫一點,以後再增加,歡迎指正。(為了不透漏一些資訊,log中包名被修改了)


1.  android.database.sqlite.SQLiteException: unable to close due to unfinalised statements


[java]
view plaincopy
  1. FATAL EXCEPTION: main  
  2. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evaluation/com.evaluation.VehicleActivity}: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements  
  3.     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)  
  4.     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)  
  5.     at android.app.ActivityThread.access$2300(ActivityThread.java:135)  
  6.     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)  
  7.     at android.os.Handler.dispatchMessage(Handler.java:99)  
  8.     at android.os.Looper.loop(Looper.java:143)  
  9.     at android.app.ActivityThread.main(ActivityThread.java:4914)  
  10.     at java.lang.reflect.Method.invokeNative(Native Method)  
  11.     at java.lang.reflect.Method.invoke(Method.java:521)  
  12.     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
  13.     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)  
  14.     at dalvik.system.NativeStart.main(Native Method)  
  15. Caused by: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements  
  16.     at android.database.sqlite.SQLiteDatabase.dbclose(Native Method)  
  17.     at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:363)  
  18.     at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)  
  19.     at android.database.sqlite.SQLiteProgram.onAllReferencesReleased(SQLiteProgram.java:116)  
  20.     at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)  
  21.     at android.database.sqlite.SQLiteProgram.close(SQLiteProgram.java:293)  
  22.     at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:133)  
  23.     at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:532)  
  24.     at com.DatabaseHelper.selectAssessInformation(DatabaseHelper.java:338)  
  25.     at com.JSKApplication.selectAssessInformation(JSKApplication.java:631)  
  26.     at com.VehicleActivity.onCreate(VehicleActivity.java:548)  
  27.     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)  
  28.     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)  
  29.     ... 11 more  

這個錯誤翻譯過來就是:不能關閉資料庫由於未完成的語句。

我從網上查了一下,最可能的原因是:你在操作sqlite資料庫時使用多線程了,但sqlite資料庫是不支援多線程操作,所以你必須實現多線程同步的機制。

我的程式出錯是因為:在後台啟動了個service定時向伺服器請求資料,並線上程中將資料插入資料庫。在某個時間段,使用者在向資料庫插入資料後,關閉資料庫,而此時線程正在向資料庫中寫入資料,就會造成上述異常。


2.android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed


[java]
view plaincopy
  1. 07-04 16:18:56.677: W/System.err(29830): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed  

約束失敗,導致這類錯誤,原因一般有兩個:

1)插入的資料有一個是主鍵,而且插入的主鍵相同;

2)插入的資料有一條資料為空白,而資料庫中定義不可為空,也會導致這樣的錯誤;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.