我正在创建一个实体(房间持久性库)类Food,在那里我想使foodId作为自增量。
@Entity
class Food(var foodName: String, var foodDesc: String, var protein: Double, var carbs: Double, var fat: Double)
{
@PrimaryKey
var foodId: Int = 0
var calories: Double = 0.toDouble()
}
如何设置foodId自增字段?
@Entity(tableName = "user")
data class User(
@PrimaryKey(autoGenerate = true) var id: Int?,
var name: String,
var dob: String,
var address: String,
var gender: String
)
{
constructor():this(null,
"","","","")
}
在这么多答案之后,这是令人难以置信的,但我最后做的有点不同。我不喜欢主键为空,我想把它作为第一个参数插入时不定义它,它也不应该是var。
@Entity(tableName = "employments")
data class Employment(
@PrimaryKey(autoGenerate = true) val id: Long,
@ColumnInfo(name = "code") val code: String,
@ColumnInfo(name = "title") val name: String
){
constructor(code: String, name: String) : this(0, code, name)
}
@Entity(tableName = "user")
data class User(
@PrimaryKey(autoGenerate = true) var id: Int?,
var name: String,
var dob: String,
var address: String,
var gender: String
)
{
constructor():this(null,
"","","","")
}