我最近繼承了一個項目,其中一個sqlite數據庫存儲在用戶sdcard(僅限表格和列,沒有內容)。對於初始安裝(和隨後的數據更新),XML文件經由SAXParser的解析存儲它的內容的分貝列像這樣:Android + sqlite插入速度提升?
的SAXParser:
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
currentElement = false;
if (localName.equals("StoreID")) {
buffer.toString().trim();
storeDetails.setStoreId(buffer.toString());
} else if (localName.equals("StoreName")) {
buffer.toString().trim();
storeDetails.setStoreName(buffer.toString());
...
} else if (localName.equals("StoreDescription")) {
buffer.toString().trim();
storeDetails.setStoreDescription(buffer.toString());
// when the final column is checked, call custom db helper method
dBHelper.addtoStoreDetail(storeDetails);
}
buffer = new StringBuffer();
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentElement) {
buffer.append(ch, start, length);
}
}
DatabaseHelper:
// add to StoreDetails table
public void addtoStoreDetail(StoreDetails storeDetails) {
SQLiteDatabase database = null;
InsertHelper ih = null;
try {
database = getWritableDatabase();
ih = new InsertHelper(database, "StoreDetails");
// Get the numeric indexes for each of the columns that we're updating
final int idColumn = ih.getColumnIndex("_id");
final int nameColumn = ih.getColumnIndex("StoreName");
...
final int descColumn = ih.getColumnIndex("StoreDescription");
// Add the data for each column
ih.bind(idColumn, storeDetails.getStoreId());
ih.bind(nameColumn, storeDetails.getStoreName());
...
ih.bind(descColumn, storeDetails.getStoreDescription());
// Insert the row into the database.
ih.execute();
} finally {
ih.close();
safeCloseDataBase(database);
}
}
加載的xml文檔長度超過6000行。當在設備上進行測試時,它會在中途停止插入(無錯誤),大約需要4-5分鐘。然而,在模擬器上,它運行速度相當快,大約20秒內將所有行寫入數據庫。我有打開db,添加數據,然後關閉時運行的日誌語句。在設備上運行時,LogCat輸出顯着較慢。有什麼我在這裏失蹤?爲什麼我的數據需要很長時間才能寫出來?我認爲改進後的InsertHelper會有所幫助,但不幸的是速度並不快。有人可以在這裏指出我的缺點嗎?
感謝您的回覆。好吧,我改變了這個:dBHelper.addtoStoreDetail(storeDetails);到這個:arrayList.add(StoreDetails);當我遍歷列表來啓動InsertHelper舞蹈時,我的arrayList只包含最後一行......爲什麼「添加」只添加最後一行?一旦這樣排序,我應該回到正軌。 – worked 2012-04-07 23:17:07
@worked:代碼在哪裏?沒有代碼,我無法幫助。 – 2012-04-08 07:58:32