我正在尝试为联赛数据实现房间数据库,但是当我尝试编译时,它不起作用。该错误似乎是在数据库文件中引发的。希望你能帮我
在Dao中,我选择所有countryProperty并将它们插入到数据库中,
@Dao
interface CountryDataBaseDao {
@Query("SELECT * FROM DatabaseCountryProperty")
fun getData() : LiveData<List<DatabaseCountryProperty>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertData(data : List<DatabaseCountryProperty>)
}
在实体中,countryProperty 实体获得额外的实体,
@Entity
data class DatabaseCountryProperty constructor(
@PrimaryKey
val id: String,
val name: String,
val imagePath: String?,
val extra: DatabaseExtraCountry?)
data class DatabaseExtraCountry constructor(
val continent : String?,
val subRegion : String?,
val worldRegion : String?,
val fifa : String?,
val iso : String?,
val iso2 : String?,
val longitude : Double?,
val latitude : Double?,
val flag : String?) {
}
fun List<DatabaseCountryProperty>.asDomainModel() : List<DevByteCountryProperty> {
return map {
DevByteCountryProperty(
id = it.id,
name = it.name,
imagePath = it.imagePath,
extra = DevByteExtraCountry(
continent = it.extra?.continent,
subRegion = it.extra?.subRegion,
worldRegion = it.extra?.worldRegion,
fifa = it.extra?.fifa,
iso = it.extra?.iso,
iso2 = it.extra?.iso2,
longitude = it.extra?.longitude,
latitude = it.extra?.latitude,
flag = it.extra?.flag
)
)
}
}
在数据库中,
@Database(entities = [DatabaseCountryProperty::class], version = 1, exportSchema = false)
abstract class DataDataBase : RoomDatabase() {
abstract val countryDataBaseDao : CountryDataBaseDao
}
private lateinit var INSTANCE: DataDataBase
fun getDatabase(context: Context): DataDataBase {
synchronized(DataDataBase::class.java) {
if (!::INSTANCE.isInitialized) {
INSTANCE = Room.databaseBuilder(context.applicationContext,
DataDataBase::class.java,
"data").build()
}
}
return INSTANCE
}
答案 0 :(得分:0)
错误是我没有嵌入注释来声明额外的是子类的事实。所以代码是,
@Entity
data class DatabaseCountryProperty constructor(
@PrimaryKey
val id: String,
val name: String,
val imagePath: String?,
@Embedded
val extra: DatabaseExtraCountry?)
data class DatabaseExtraCountry constructor(
val continent : String?,
val subRegion : String?,
val worldRegion : String?,
val fifa : String?,
val iso : String?,
val iso2 : String?,
val longitude : Double?,
val latitude : Double?,
val flag : String?) {
}
答案 1 :(得分:0)
将这些添加到您的构建 gradle 中。
插件中的 id 'kotlin-kapt'
kapt "androidx.room:room-compiler:2.3.0" in your dependencies
尝试一下