gpt4 book ai didi

android - Greendao Autoincrement插入记录不起作用

转载 作者:行者123 更新时间:2023-12-04 16:05:23 26 4
gpt4 key购买 nike

我正在使用 greendao 3.2 并制作实体和数据库。它一切正常。
但是在创建必须自动递增的 Id 属性时我遇到了麻烦。我知道如何在 SQL 中做到这一点,但使用 greendao 它给了我更多的困难。

我已宣布我的实体正常。让我给你举个例子。

@Entity
public class User {

// this will make your id autoincremented
@org.greenrobot.greendao.annotation.Id (autoincrement = true)
private Long Id;
private String name;
@Generated(hash = 690585871)
public User(Long Id, String name) {
this.Id = Id;
this.name = name;
}
@Generated(hash = 586692638)
public User() {
}
public Long getId() {
return this.Id;
}
public void setId(Long Id) {
this.Id = Id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}

并插入像
user = new User();
user.setName("Hello Green Dao");
Long id = userDao.insertOrReplace(user);

但它与 Id 0 的重叠记录一次又一次。它没有按预期工作。

请告诉我是什么原因。我也尝试过使用插入,但它显示相同的结果。请帮助我陷入困境。

最佳答案

我已经使用它如下,它工作正常。

@Entity(nameInDb = "cities")
public class City {
@Id(autoincrement = true)
private Long id;
....
}

唯一的区别是您使用了 Id , 也许用大写 I使它成为保留字并导致此问题。或者你应该把它上面的注释简要介绍给 @Id而不是完整的包路径。我知道这听起来很奇怪,但值得一试。

关于android - Greendao Autoincrement插入记录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48983527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com