English 中文(简体)
创建OrUpdate(OrUpdate)总是在数据库中与ORMLite(ORML)一起创造新的条目。
原标题:createOrUpdate() always creates a new entry in the database with ORMLite

在我的安乐团项目中,ORMLite正在发挥海滩的作用。 Im从一个网络服务器下载数据并将其输入数据库。 我的物体上的Im打createOrUpdate,但数据库中出现了重复。 数据库的条目相同,但主要钥匙除外(这只是一个自动增加的分类)。 我认为,由于我的第二个目标尚未具有首要关键意义,因此ORMLite认为两者不同,尽管所有其他领域相同。

是否有任何人知道这是否真实?

最佳回答

页: 1 除非标的already有一栏id。 ORMLite>确定数据库中是否存在这种编号是为了逐个查询。 该法典规定:

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);
}

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

如果不存在,这是在数据库中制造一个项目的方便方法。 id从数据论据中提取,数据库中逐个查询。 如果数据库中存在同样的网,数据库中的所有栏目都将从数据参数的田中更新。 如果补偿金(或零件或部分其他违约价值)无效,或数据库中确实存在,则将在数据库中设定目标。 这也意味着,你的数据项目必须有一个界定的近似领域。

问题回答

似乎你手工需要根据你的实际独特识别资料,抽取现有条目的出处,然后取<代码>update数据库。 独有的URLs实例:

CacheItem newEntry = fetchEntry("..."); // fetch it here
List<CacheItem> existing = mDao.queryForEq("url", newEntry.getUrl());
if (existing.size() > 0) {
    int id = existing.get(0).getId();
    newEntry.setId(id);
    mDao.update(newEntry);
} else mDao.create(newEntry);

您为正常工作确定了主要的关键。

@DatabaseField(columnName = "id",uniqueIndex = true) private int id;

为我工作。 例外情况被抛在背景之下,但它运作良好:D





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签