在糖中自动增加主键

时间:2016-05-06 16:33:40

标签: android sugarorm

如何在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;
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用“@Table”创建一个自定义表格,但是您应该创建一个长类型的“id”供ORM使用。最好的方法是让糖做所有的工作。

Sugar 创建一个id字段(您不需要在您的类中包含),您可以使用“getId()”来访问它:

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