1

我正在使用 ORMLite 来管理包含数据收集应用程序的查找值列表的数据库表。这些查找值会定期从远程服务器更新。但是,我希望能够在创建或更新记录时将数据保留在特定列中,因为我想存储与每个查找值关联的使用计数(特定于设备)。这是我更新记录的方式:

        //build list of new records
        final List<BaseLookup> rows = new ArrayList<BaseLookup>();
        for (int i = 0; i < jsonRows.length(); i++) {
            JSONObject jsonRow = jsonRows.getJSONObject(i);

            //parse jsonRow into a new BaseLookup object and add to rows
            ...
        }

        //add the new records
        dao.callBatchTasks(new Callable<Void>() {
            public Void call() throws Exception {
                for (BaseLookup row : rows) {
                    //this is where I'd like to preserve the existing
                    //value (if any) of the "usageCount" column

                    Dao.CreateOrUpdateStatus result = dao.createOrUpdate(row);
                }
                return null;
            }
        });

我考虑过尝试在循环中单独获取和合并每条记录,但这似乎表现不佳(有些表是几千条记录)。有没有更简单或更综合的方法来实现这一点?

4

1 回答 1

1

我希望能够在创建或更新记录时将数据保留在特定列中,因为我想存储与每个查找值关联的使用计数(特定于设备)

如果您必须从 JSON 数据更新某些列,但又想将其设置为usageCountusageCount + 1那么您有几个选择。

  • dao.updateBuilder(); 您可以使用方法和类构建更新语句UpdateBuilder,然后将列更新为其新值以及id 匹配的位置usageCountusageCount + 1您应该注意返回值以确保更新了一行。如果没有,那么您创建对象。

  • 但是,这样做会更容易:

    1. BaseLookup从数据库中获取
    2. 如果为 null,则调用dao.create()以保留新条目
    3. 否则更新列并增加usageCount
    4. 并用dao.update(...)
于 2013-07-24T13:57:19.610 回答