安卓 SQLite error:SQLite database locked exception while compling : PRAGMA journal_mode .....

來源:互聯網
上載者:User

標籤:android   io   os   資料   sp   c   問題   on   cti   

項目中頻繁的切換Tab鍵,會頻繁地從資料庫讀取資料,這是報出這樣的錯誤:

解決方案是在國外的某論壇找到的 :

在插入資料的時候,首先:

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f, null);
db.beginTransaction();   ////開啟一個事務
db.insert(HJZ_SQLiteOpenHelper.TABLE_NAME_KP, null, cv);
db.setTransactionSuccessful(); //必須加這一句,否則插入不了資料
db.endTransaction();  ////關閉一個事務
db.close();

 

原因:是4.0以前的版本db.close();會結束事務,而Jelly Bean 以後的版本因為安全性的問題,必須結束即endTransactiony以後才能再次訪問本機資料庫。哎。。苦逼的百度啊。。另外,吐槽一下不向下相容的Android

PS:android中,對資料庫進行大量操作時,建議先開啟會話:

db.beginTransaction();

//批量insert或者update等
db.setTransactionSuccessful();
db.endTransaction();

 

這樣之後,在大資料量的處理是灰常的高效,

實際中,減少了3~10倍的時間。

 

另外,在重寫ContentProvider時,不要針對資料的讀寫加同步了,因為底層資料庫已經做了同步保護,上層再加上同步保護多餘,而且非常影響效率。

安卓 SQLite error:SQLite database locked exception while compling : PRAGMA journal_mode .....

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.