android – createOrUpdate()始终使用ORMLite在数据库中创建一个新条目

android – createOrUpdate()始终使用ORMLite在数据库中创建一个新条目,第1张

概述在我的 Android项目中,ORMLite用作缓存.我正在从Web服务器下载数据并将其放入数据库.我在我的对象上调用createOrUpdate,但数据库中出现重复的对象.数据库条目是相同的,除了主键(这只是一个自动递增的整数).我认为,由于我的第二个对象还没有一个主键,ORMLite认为这两个对象是不同的,即使每个其他的字段是相同的. 有人知道这是否是真的? 您不应该调用createOrUpd 在我的 Android项目中,Ormlite用作缓存.我正在从Web服务器下载数据并将其放入数据库.我在我的对象上调用createOrUpdate,但数据库中出现重复的对象.数据库条目是相同的,除了主键(这只是一个自动递增的整数).我认为,由于我的第二个对象还没有一个主键,Ormlite认为这两个对象是不同的,即使每个其他的字段是相同的.

有人知道这是否是真的?

解决方法 您不应该调用createOrUpdate,除非您的对象已经设置了一个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);}

我将扩大javadocs来解释这个更好.他们在那里很弱.抱歉.我已经将它们更新为:

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.

总结

以上是内存溢出为你收集整理的android – createOrUpdate()始终使用ORMLite在数据库中创建一个新条目全部内容,希望文章能够帮你解决android – createOrUpdate()始终使用ORMLite在数据库中创建一个新条目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1132218.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存