简体   繁体   中英

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

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 :

    @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 . Why do I need to provide a 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

@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. 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.

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.

So I would try setting the uid to be initialized to 0 or make it nullable and set to null.

Hope that helps!

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

  • 1st Create a Database instance 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

     @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

  • Add data in Shader and insertAll Like Below

     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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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