我正在解析一個xml,解析完這個xml後,我將這些記錄存儲在數據庫中。我使用SQLite作爲數據庫。我的數據是以幻燈片的形式在XML中。當我存儲在數據庫中的一些幻燈片,我得到重複的條目。例如,如果我的xml包含3張幻燈片,則在數據庫6中存儲有不同的項目標識,因此每張幻燈片都會保存2次。這是我在做什麼在數據庫中獲取重複條目
ArrayList<SlideShowItem>slideItems=Utils.database.getSlideItemOfUrl(Constants.StoriesTable,tempSlideShow.getFullStoryUrl().substring(0, index - 1), type);
if (slideItems == null) {
Log.d("store in DB: ", " when SlideItems == null ");
Log.d("SlideShow Title: ", tempSlideShow.getTitle());
Log.d("SlideShow pub Date: ", tempSlideShow.getPubDate());
Utils.database.storeSlideItem(Constants.StoriesTable, myUrl, tempSlideShow.getSlideShow(), null);
//Utils.topStorySlidesArrayList = slideItems;
} else {
//Log.d("SlideShow Title: ", tempSlideShow.getTitle());
Log.d("Already in DB ", " when SlideItems is not null ");
Utils.topStorySlidesArrayList = slideItems;
}
請任何幫助apprecitaed。我想我正在把一些檢查錯誤。請在這方面幫助我。
Utils.database.storeSlideItem包含此代碼:
public synchronized void storeSlideItem(String tableName, String url, ArrayList<SlideShowItem> list, String type) {
System.out.println("size of the Array list: " + list.size());
String newType = null;
if (type == null) {
newType = "List";
}else{
newType = type;
}
try {
for (int i = 0; i < list.size(); i++) {
SlideShowItem item = list.get(i);
String itemUrl = url + i;// Unique URL for the DB;
String imgString = null;
Drawable drawable = item.getImage();
if (item.getBody() != null) {
item.setBody(item.getBody().replace('\'', '`'));
// replace as it create syntax error for storing data
}
if (item.getSubTitle() != null) {
item.setSubTitle(item.getSubTitle().replace('\'', '`'));
}
if (drawable != null) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
imgString = Base64.encodeBytes(b);
}
if (isOpen()) {
myDB.execSQL("INSERT INTO " + tableName + "(" + column[1] + "," + column[2] + "," + column[3] + "," + column[4] + "," + column[6]
+ "," + column[7] + ",type) VALUES('" + itemUrl + "','" + item.getSubTitle() + "','" + item.getBody() + "','"
+ item.getImagePath() + "','" + item.getIndex() + "','" + imgString + "','" + newType + "Slide')");
if (item.getBody() != null) {
item.setBody(item.getBody().replace('`', '\''));// " ' "
// replace as it create syntax error for storing data
}
if (item.getSubTitle() != null) {
item.setSubTitle(item.getSubTitle().replace('`', '\''));
}
if (tableName.equals(Constants.StoriesTable)) {
item.setItemId(getItemID(tableName, itemUrl));
Utils.hashListStoriesIds.put(itemUrl, item.getItemId());
if (imgString != null) {
Utils.hashListImages.put(item.getItemId(), new Boolean(true));
} else {
Utils.hashListImages.put(item.getItemId(), new Boolean(false));
}
}
}
}
} catch (Exception e) {
Log.e("Error", "Exception: storeSlideItem type " + e.toString());
} finally {
closeConnection();
}
}
請顯示您的'Utils.database.storeSlideItem'方法的代碼。 – THelper
@THelper我也提供了這個代碼..現在看看你是否可以幫忙。 –
我注意到你首先將東西存入你的數據庫,然後改變一些項目的屬性? (setBody,SetSubTitle,SetItemId)。不應該改變屬性後保存到數據庫? – THelper