Greendao 3,使用户表没有重复的用户名

时间:2018-03-07 10:27:57

标签: android database sqlite greendao greendao3

在搜索了这个问题的解决方案并找不到它之后,我问了这个问题。 我已经构建了一个包含三列的数据库:用户名,密码和年龄。 我能够新用户并更新它们,但我希望它不可能插入两个相同的用户名。我知道如何在sqlite中做到这一点,但在greendao它只是不适合我。谢谢。

这是我的UsersClass:

@Entity(nameInDb = "users")
public class Users{

    @Id(autoincrement = true)
    private Long id;

    @NotNull
    private String userName;

    @NotNull
    private String password;

    @NotNull
    private int age;
}

1 个答案:

答案 0 :(得分:0)

@Unique注释应该适合您。

@Entity(nameInDb = "users")
public class Users{

    @Id(autoincrement = true)
    private Long id;

    @Unique
    private String userName;

    @NotNull
    private String password;

    @NotNull
    private int age;
}

如果您需要进一步自定义,可以向@Entity注释添加选项

@Entity(
    ...
    // Define indexes spanning multiple columns here.
    indexes = {
            @Index(value = "name DESC", unique = true)
    },
    ...
)