ormlite怎么更新数据源码解析?

这里并不是什么好深奥的解析,就是翻译的过程。

本人对数据库一直都是小白,所以一谈到数据库都是头痛,今天这里讲的是ormlite,在项目中遇到的问题拿来讲另一种是顺便记录一下。

操作过程中,需要保存数据用到的方法

createOrUpdate()

就是创建和更新:意思是如果存在就更新,不存在就创建,现在的需求是相同的数据类型就更新同一个,不是就add新的数据,遇到的问题就一直都在创建,就算是再同一条数据,当时就郁闷了,反复思考查看代码逻辑,没有问题,但是就是不能更新同一个,还是别个指导了一下,如果自己遇到这个问题话,不知道能不能解决,说不定卡死再哪里。

问题解决:

主要原因是id为null,这里没有写自动增加,再添加更新数据的时候为空。看看ormlite方法说明:

/**
 * This is a convenience method for creating an item in the database if it does not exist. The id is extracted from
 * the data parameter 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 <i>must</i> have an id field defined.
 * 
 * @return Status object with the number of rows changed and whether an insert or update was performed.
 */
public CreateOrUpdateStatus createOrUpdate(T data) throws SQLException;

百度解释:

/**

*这是一种在数据库中创建不存在项的方便方法。从中提取ID

*对数据库进行数据参数和ID查询。如果数据库中存在具有相同ID的行

*然后数据库中的所有列都将从数据参数中的字段更新。如果ID为空

*(或0或其他一些默认值)或数据库中不存在,则将在

*数据库。这也意味着您的数据项<i>必须定义ID字段。

*@返回状态对象,更改了行数以及是否执行了插入或更新。

*/

看到翻译之后,相信有点眉目了。对,这个方法对数据库插座是根据id来进行查询操作的,如果id为空,是视为新的数据。

之后又查看了查询,删除都是根据id来操作,如果没有传入参数的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值