sugar orm中的自动递增主键

Auto increment primary key in sugar orm

Sugar模型如何定义自增主键? Do Sugar 自动为每条记录创建唯一 ID

import com.orm.SugarRecord;

public class Customers extends SugarRecord {

    int id; // this field must be auto increment primary key
    String name;
    String tel;
    String mobile;
    String address;
    String create_date;

    public Customers(){}

    public Customers(int id, String name, String tel, String mobile, String address, String create_date){
        this.id = id;
        this.name = name;
        this.tel = tel;
        this.mobile = mobile;
        this.address = address;
        this.create_date = create_date;
    }
}

您可以创建一个自定义 table 我用“@Table”注释您的 class,但是您应该为 ORM 创建一个长类型的 "id"一起工作。最好的方法是让糖完成所有工作。

Sugar 创建一个 id 字段(您不需要包含在您的 class 中)并且您可以使用 "getId()" 访问它,例如:

Customers customer = Customers.findById(Customers.class, 1)
long customer_id = customer.getId();