2012-08-08 67 views
0

我正在開發Android應用程序,在這裏我有大量的數據大約10000條記錄與服務器中的10個字段,我需要獲取這些數據並將其存儲在我的本地數據庫,所以爲此,我嘗試通過以json的形式獲取數據並解析它並逐個插入數據庫來實現數據,但是下載數據需要更少的時間,但有更多時間插入到數據庫中,一段時間後我才知道我插入到數據庫一個接一個,所以插入操作循環基於已獲得的記錄總數。我試圖尋找替代方案,我無法爲此得到方法,所以我請求你給我建議和片段給我實現這一點。將服務器數據轉換爲Sqlite數據庫

感謝你

回答

0

使用交易多重刀片包裝成一個操作,這是一個很大更快:Improve INSERT-per-second performance of SQLite?

List<Item> list = getDataFromJson(); 
SQLiteDatabase db = getDatabase(); 

db.beginTransaction(); 
try { 
    // doing all the inserts (into memory) here 
    for(Item item : list) { 
     db.insert(table, null, item.getContentValues()); 
    } 
    // nothing was actually inserted yet 
    db.setTransactionSuccessful(); 
} finally { 
    // all inserts happen now (if transaction was set to successful) 
    db.endTransaction(); 
} 
相關問題