简体   繁体   English

如何在 Android 房间数据库中创建新记录?

[英]How do I create a new record in Android Room database?

This seems like a lazy question, but the offical docs literally don't explain how to do this.这似乎是一个懒惰的问题,但官方文档确实没有解释如何做到这一点。

https://developer.android.com/training/data-storage/room https://developer.android.com/training/data-storage/room

I have an entity like so:我有一个这样的实体:

@Entity
data class Shader(
    @PrimaryKey(autoGenerate = true) val uid: Int,
    val name: String,
    val shaderMainText: String,
    val paramsJson: String?
)

And some methods on my Dao :还有我的Dao上的一些方法:

    @Insert
    fun insertAll(vararg shaders: Shader)

    @Delete
    fun delete(shader: Shader)

    @Update
    fun update(shader: Shader)

How do I make a new record and insert it?如何制作新记录并插入?

I've gotten so far as this:我已经做到了这一点:

val record = Shader(name="Foo", shaderMainText="bar", paramsJson=null)

But it complains that I'm missing the argument uid .但它抱怨说我遗漏了参数uid Why do I need to provide a uid ?为什么我需要提供一个uid It's supposed to be auto-generated?应该是自动生成的吧?

An example of creating a new record would be appreciated.创建新记录的示例将不胜感激。

You can set 0 for the default value of your uid field您可以将uid字段的默认值设置为0

@Entity
data class Shader(
    @PrimaryKey(autoGenerate = true) val uid: Int = 0,
    val name: String,
    val shaderMainText: String,
    val paramsJson: String?
)

When autoGenerate is true , Room ignores the default value, so it creates auto-incremented values.autoGeneratetrue时,Room 会忽略默认值,因此它会创建自动递增的值。 Finally, this doesn't give you a compilation error:最后,这不会给你一个编译错误:

val record = Shader(name="Foo", shaderMainText="bar", paramsJson=null)

The docs say:文档说:

If the field type is long or int (or its TypeConverter converts it to a long or int), Insert methods treat 0 as not-set while inserting the item.如果字段类型为 long 或 int(或其 TypeConverter 将其转换为 long 或 int),则插入方法在插入项目时将 0 视为未设置。

If the field's type is Integer or Long (or its TypeConverter converts it to an Integer or a Long), Insert methods treat null as not-set while inserting the item.如果字段的类型是 Integer 或 Long(或其 TypeConverter 将其转换为 Integer 或 Long),则插入方法在插入项目时将 null 视为未设置。

So I would try setting the uid to be initialized to 0 or make it nullable and set to null.所以我会尝试将要初始化的 uid 设置为 0 或使其可为空并设置为 null。

Hope that helps!希望有帮助!

https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey#autoGenerate() https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey#autoGenerate()

You Can Also try like this你也可以这样试试

The best practice is you always create an instance Class最佳做法是始终创建一个instance Class

  • 1st Create a Database instance class 1st 创建一个数据库实例 class

     @Database(entities = [Shader::class], version = 1) abstract class DatabaseHelper: RoomDatabase() { companion object { private var instance: DatabaseHelper? = null fun getInstance(context: Context?): DatabaseHelper? { if (instance == null) { synchronized(DatabaseHelper::class) { if (context.= null) { instance = Room.databaseBuilder(context,applicationContext: DatabaseHelper:.class,java. "database.db").allowMainThreadQueries():build() } } } return instance } } abstract fun getDao()? DBDao? }
  • Create Your Shader data class like below像下面这样创建你的Shader data class

     @Entity(tableName = "shader") data class Shader( @PrimaryKey(autoGenerate = true) var id: Int?, val name: String, val shaderMainText: String, val paramsJson: String? ) { constructor(name: String, shaderMainText: String, paramsJson: String?): this( null, name, shaderMainText, paramsJson ) }

    Here you want to create constructor在这里你要创建constructor

  • Add data in Shader and insertAll Like BelowShader中添加数据, insertAll如下

     val record = Shader(name="Foo", shaderMainText="bar", paramsJson=null) DatabaseHelper.getInstance(this)?.getDao()?.insertAll(record)

And Your Data added is successfully并且您的数据添加成功

在此处输入图像描述

There should be always a @PrimaryKey(autoGenerate = false) while inserting data in the DB在数据库中插入数据时,应该始终有一个 @PrimaryKey(autoGenerate = false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM