我正在解析xml,在解析了xml之后我将记录存储在DB中。我使用SQLite作为DB。我的数据采用xml中的幻灯片形式。当我在数据库中存储一些幻灯片时,我得到重复的条目。例如,如果我的xml包含3张幻灯片,则在DB 6中存储不同的项目ID,因此每张幻灯片都会保存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;
}
请感谢任何帮助。我想我错了一些检查。请帮助我。
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();
}
}
答案 0 :(得分:0)
您的商品中是否有唯一的ID字段?如果是这样,您可以将其作为数据库模式中的主键,这将强制所有条目不同或在添加重复时抛出异常,以便您可以追踪问题。