createOrUpdate()总是创建与ORMLite数据库中的新条目 [英] createOrUpdate() always creates a new entry in the database with ORMLite

查看:2620
本文介绍了createOrUpdate()总是创建与ORMLite数据库中的新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid项目,ORMLite被用作高速缓存。林从Web服务器下载数据,并把它在数据库中。我打电话 createOrUpdate 我的对象,但是重复的出现在数据库中。该数据库条目是相同的,除了主键(这仅仅是一个自动递增的整数)。我想,既然我的第二个对象还没有一个主键,ORMLite认为两成是不同的,即使其他所有字段是相同的。

In my Android project, ORMLite is functioning as a cache. I'm downloading data from a web server and placing it in the database. I'm calling createOrUpdate on my objects, but duplicates are appearing in the database. The database entries are identical except for the primary key (which is simply an auto incremented integer). I think that since my second object doesn't yet have a primary key, ORMLite considers the two as being different, even though every other field is identical.

有谁知道这是真的吗?

推荐答案

您不应该叫 createOrUpdate 除非你的对象的的有id字段集。该方法 ORMLite 确定它是否存在于数据库是做一个查询通过ID就可以了。在code的作用:

You should not be calling createOrUpdate unless your object already has an id field set. The way ORMLite determines whether or not it exists in the database is to do a query-by-id on it. The code does:

ID id = extractId(data);
// assume we need to create it if there is no id  <<<<<<<<<<<<<<<<<<<<<<<
if (id == null || !idExists(id)) {
    int numRows = create(data);
    return new CreateOrUpdateStatus(true, false, numRows);
} else {
    int numRows = update(data);
    return new CreateOrUpdateStatus(false, true, numRows);
}

我会扩大的javadoc来解释这更好的。他们的非常的薄弱那里。抱歉。我已经更新了他们是:

I'll expand the javadocs to explain this better. They are very weak there. Sorry. I've updated them to be:

这是一个便捷方法创建项目在数据库中,如果它不存在。的ID从数据提取的参数和查询通过ID由在数据库上。如果一排中具有相同id数据库存在那么所有数据库中的列将在从所述数据参数的字段被更新。如果id为空(或0或一些其它的默认值),或在随后的对象将在数据库中创建的数据库中不存在。这也意味着你的数据项都必须有一个ID字段定义。

This is a convenience method for creating an item in the database if it does not exist. The id is extracted from the data argument and a query-by-id is made on the database. If a row in the database with the same id exists then all of the columns in the database will be updated from the fields in the data parameter. If the id is null (or 0 or some other default value) or doesn't exist in the database then the object will be created in the database. This also means that your data item must have an id field defined.

这篇关于createOrUpdate()总是创建与ORMLite数据库中的新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆