android如何更新领域数据库,如果值已经存在

发布时间: 2016/9/28 1:21:31
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

如何更新值,如果它已经存在于数据库领域; 我试着去 build 应用程序来显示一些语录有名的作家,该应用程序会有一些作者和每个作者会有一些报价,当我在第一次运行该应用程序 time 它完美地运行,但当我尝试运行它再一次它显示我的值已存在错误。
这是我的模型类

public class Author extends RealmObject {
@PrimaryKey
private String name;
private RealmList<Quotes>quote;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Author() {

}

public RealmList<Quotes> getQuote() {
    return quote;
}

public void setQuote(RealmList<Quotes> quote) {
    this.quote = quote;
}

public Author(String name, RealmList<Quotes> quote) {

    this.name = name;
    this.quote = quote;

}

}

public class Categories extends RealmObject {
@PrimaryKey
private String category;

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public Categories() {

}

public Categories(String category) {

    this.category = category;
}

}

public class Quotes extends RealmObject {
private String Quote;
private Categories category;

public Categories getCategory() {
    return category;
}

public void setCategory(Categories category) {
    this.category = category;
}

public String getQuote() {
    return Quote;
}

public void setQuote(String quote) {
    Quote = quote;
}

public Quotes() {

}

public Quotes(Categories category, String quote) {

    this.category = category;
    Quote = quote;
}

}

这是我的 mainActivity 哪里设置数据库和得到的错误︰ 值已经存在

public class MainActivity extends AppCompatActivity {
Realm mRealm;
Categories wisdom;
Categories fear;
Categories motivation;
Categories life;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RealmConfiguration configuration=new RealmConfiguration.Builder(this).build();
    Realm.setDefaultConfiguration(configuration);
    mRealm=Realm.getDefaultInstance();

    mRealm.beginTransaction();


    wisdom=mRealm.createObject(Categories.class);
    fear=mRealm.createObject(Categories.class);
    wisdom.setCategory("wisdom");
    fear.setCategory("fear");
    life.setCategory("Life");
    motivation.setCategory("Motivation");

    Author john_green=mRealm.createObject(Author.class);
    john_green.setName("John Green");

    Quotes john_green_1=mRealm.createObject(Quotes.class);
    john_green_1.setQuote("My thoughts are stars I can't fathom into constellations.");
    john_green_1.setCategory(motivation);
    john_green.getQuote().add(john_green_1);

    Quotes john_green_2=mRealm.createObject(Quotes.class);
    john_green_2.setQuote("What is the point of being alive if you don’t at least try to do something remarkable?");
    john_green_2.setCategory(motivation);
    john_green.getQuote().add(john_green_2);

    mRealm.commitTransaction();
}

}

解决方法 1:

试第一,找到它,如果它没有找到 (返回 null) 创建对象。

mRealm=Realm.getDefaultInstance();
mRealm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        Category wisdom = realm.where(Categories.class).equalTo("category", "wisdom").findFirst();
        if(wisdom == null) {
            wisdom = realm.createObject(Categories.class);
            wisdom.setCategory("wisdom");
        }
        //more code
    }
});
官方微信
官方QQ群
31647020