Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

Android Room @insert, how to insert a row with column filling, when creating a database

If you pass a single id value, then there will be no errors everything works. I pass When the remaining values are wordEn, wordRu, outputs an error: No value passed for parameter 'wordEn' No value passed for parameter 'wordRu' how do I insert a row into a database with columns filled in? See where <-!!!

@Dao
interface WordDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)            
    suspend fun insert(id: Word, wordEn: Word, wordRu: Word)
}

@Entity(tableName = "word_table")
data class Word (

    @PrimaryKey
    var id: Int,
    var wordEn: String,
    var wordRu: List<String>
)


@Database(entities = [Word::class], version = 1, exportSchema = false)
abstract class AppRoomDatabase : RoomDatabase() {

....
....
                    val instance = Room.databaseBuilder(
                        context.applicationContext,
                        AppRoomDatabase::class.java,
                        "app_room_database"
                    )
                        .addCallback(WordDatabaseCallback(scope))
                        .build()
                    INSTANCE = instance
                    instance
                }
        }
        private class WordDatabaseCallback(
            private val scope: CoroutineScope
        ) : RoomDatabase.Callback() {
           
            override fun onOpen(db: SupportSQLiteDatabase) {
                super.onOpen(db)
              
                INSTANCE?.let { database ->
                    scope.launch(Dispatchers.IO) {
                        populateDatabase(database.wordDao())
                    }
                }
            }
        }

        suspend fun populateDatabase(wordDao: WordDao) {
          
            //TEST Fill the database with random string
....
....
                var randomInt: Int = randomInt()
                var randomString: String = randomString()
                var list: List<String> = list()

                wordDao.insert(Word(randomInt , "$a _$randomString",list)) <- !!!
            }
            //TEST end
}
question from:https://stackoverflow.com/questions/65868031/android-room-insert-how-to-insert-a-row-with-column-filling-when-creating-a-d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The insert statement is not correct

@Dao
interface WordDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)            
    suspend fun insertWord(word: Word)

    @Insert(onConflict = OnConflictStrategy.IGNORE)            
    suspend fun insertWords(words: List<Word>)

    @Insert(onConflict = OnConflictStrategy.IGNORE)            
    suspend fun insertWords1(varargs words: Word)
}

Since Word is an entity

@Entity(tableName = "word_table")
data class Word (
    @PrimaryKey
    var id: Int,
    var wordEn: String,
    var wordRu: List<String>
)

Room ?? Coroutines


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...