如何将主键作为Room Persistence lib的自动增量 [英] How to make primary key as autoincrement for Room Persistence lib
本文介绍了如何将主键作为Room Persistence lib的自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个Entity(Room Persistence lib)类Food,我想在其中将foodId
作为自动增量.
I am creating a Entity(Room Persistence lib) class Food, where i want to make foodId
as autoincrement.
@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
自动递增字段?
How can i set foodId
an autoincrement field?
推荐答案
您需要使用autoGenerate
属性
您的主键注释应如下:
@PrimaryKey(autoGenerate = true)
这篇关于如何将主键作为Room Persistence lib的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文