Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update data:不能从其他类型更新到0 #32

Closed
iamwent opened this issue May 21, 2015 · 3 comments
Closed

Update data:不能从其他类型更新到0 #32

iamwent opened this issue May 21, 2015 · 3 comments

Comments

@iamwent
Copy link

iamwent commented May 21, 2015

对以下代码:
Video video = new Video();
video.setCategory(targetCategory);
video.update(id);

targetCategory是int,如果targetCategory==0,则无法从原有的category更新到0

@BelongsH
Copy link

BelongsH commented Jul 7, 2015

我也发现了一个这样的问题。

@sinyu890807
Copy link
Contributor

设计如此,原因我以前在博客上也解释过,如果有更好的办法欢迎建议。我把博客上解释的贴过来一下。
比如News类里面有title,message和commentcount这样几个字段,那么现在写了下面这样一段代码来更新:
News n = new News();
n.setTitle("aaa");
n.update(id);
那么请问,这里我想更新的是哪个信息?肯定是title吧。OK,那么这个时候message的值是什么呢?是null,commentcount是什么呢?是0。也就是说如果这里不加判断的话,那么就会把message和commentcount的值都清空了,不可能要求开发者,想更新一个title的值,然后要把message,commentcount等其它字段每个值都set一遍吧?所以我设计的时候就是判断如果调用update这个方法更新的时候字段的值是默认值,那就不更新。想要更新成默认值,调用setToDefault()方法。
你上面的问题,调用video.setToDefault("category");就可以解决了

@SuperZiLu
Copy link

我草 原来这么多人遇到跟我一样的问题 终于找到解释了
但是我用ContentValues 这种方式去更新的时候是可以修改成功的

ContentValues values = new ContentValues();
values.put("status", "0");
int i2 = LitePal.updateAll(toryBean.class, values, "status = ? and textId = ?", "1", "1234");

大概这么个写法 是可以的
吐槽一句 数据库高手的文档太老了 好多都改了 这么重要的点也不在文档里说

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants