有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何不将记录复制到ormlite

我有一个问题。我的应用程序连接到WebService并下载啤酒厂的数据。这些数据被保存到ORMLETDB

我应该怎么做才能使数据库不复制相同的记录

窗口。java

dbHelper = (DatabaseHelper) OpenHelperManager.getHelper(getApplication(), DatabaseHelper.class);

final RuntimeExceptionDao studDao = dbHelper.getStudRuntimeExceptionDao();
BeerDataBaseTemplate wdt = new BeerDataBaseTemplate();

for (Datum i : breweries.getData()) {
    if (i.getName() != null && i.getAbv() != null && i.getDescription() != null && i.getLabels() != null) {
          studDao.createIfNotExists(new BeerDataBaseTemplate("" + i.getName(), i.getAbv(), i.getDescription(), i.getLabels().getMedium().toString(), i.getLabels().getLarge().toString(), false));
    } else if (i.getName() != null && i.getAbv() != null && i.getDescription() == null && i.getLabels() != null) {
         studDao.createOrUpdate(new BeerDataBaseTemplate("" + i.getName(), i.getAbv(), "Brak danych", i.getLabels().getMedium().toString(), i.getLabels().getLarge().toString(), false));
    } else if (i.getName() != null && i.getAbv() != null && i.getDescription() == null && i.getLabels() == null) {
         studDao.createOrUpdate(new BeerDataBaseTemplate("" + i.getName(), i.getAbv(), "Brak danych", "Brak zdjęcia", "Brak zdjęcia", false));
     } else if (i.getName() != null && i.getAbv() == null && i.getDescription() == null && i.getLabels() == null) {
         studDao.createOrUpdate(new BeerDataBaseTemplate("" + i.getName(), "Brak danych", "Brak danych", "Brak zdjęcia", "Brak zdjęcia", false));
    } else if (i.getName() == null && i.getAbv() == null && i.getDescription() == null && i.getLabels() == null) {
        studDao.createOrUpdate(new BeerDataBaseTemplate("Brak danych", "Brak danych", "Brak danych", "Brak zdjęcia", "Brak zdjęcia", false));
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您要做的一件事是将传入记录的所有ID放入Collection,然后执行以下操作:

    GenericRawResults<String[]> results
        = studDao.queryRaw("SELECT id FROM stud WHERE id IN ?", newIdCollection);
    

    然后你就会知道哪些ID是新的,哪些是重复的。然后可以更新(或忽略)DUP,并创建ID在表中不存在的新DUP

    确保批量更新并创建一个批处理操作,这将运行得更快