Reputation: 613
I try to create Room database and insert values to it, but I got this error:
public abstract class WeatherDatabase extends androidx.room.RoomDatabase { ^error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mDatabase in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mCallbacks in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mQueryExecutor in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mTransactionExecutor in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mOpenHelper in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mInvalidationTracker in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mCloseLock in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mSuspendingTransactionId in androidx.room.RoomDatabaseerror: Cannot figure out how to save this field into database. You can consider adding a type converter for it. - mBackingFieldMap in androidx.room.RoomDatabaseerror: Cannot find getter for field. - mAllowMainThreadQueries in androidx.room.RoomDatabaseerror: Cannot find getter for field. - mCloseLock in androidx.room.RoomDatabaseerror: Cannot find getter for field. - mSuspendingTransactionId in androidx.room.RoomDatabaseerror: Cannot find getter for field. - mBackingFieldMap in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mQueryExecutor in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mTransactionExecutor in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mOpenHelper in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mInvalidationTracker in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mAllowMainThreadQueries in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mCloseLock in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mSuspendingTransactionId in androidx.room.RoomDatabaseerror: Cannot find setter for field. - mBackingFieldMap in androidx.room.RoomDatabaseC:\Users\ceran\Desktop\Projekty\Android\SimpleWeatherApp\app\build\tmp\kapt3\stubs\debug\com\example\simpleweatherapp\database\WeatherDao.java:11: error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: databaseweather) public abstract androidx.lifecycle.LiveData<com.example.simpleweatherapp.database.DatabaseWeather> getWeather(); ^C:\Users\ceran\Desktop\Projekty\Android\SimpleWeatherApp\app\build\tmp\kapt3\stubs\debug\com\example\simpleweatherapp\database\WeatherDao.java:14: error: com.example.simpleweatherapp.database.WeatherDao is part of com.example.simpleweatherapp.database.WeatherDatabase but this entity is not in the database. Maybe you forgot to add com.example.simpleweatherapp.database.DatabaseWeather to the entities section of the @Database?
It's long, but it's repeated the same sentence "Cannot figure out how to save this field into database. You can consider adding a type converter for it." What should I do? Here is my code:
@Entity(tableName = "current_weather")
data class DatabaseWeather constructor(
val name: String,
val lon: Double,
val lat: Double,
val description: String,
val icon: String,
val temp: Double,
val tempFeelsLike: Double,
val humidity: Int,
val pressure: Int
){
@PrimaryKey(autoGenerate = false)
var id: Int = WEATHER_ID
}
fun DatabaseWeather.asDomainModel(): CurrentWeather{
return CurrentWeather(
name = this.name,
lon = this.lon,
lat = this.lat,
description = this.description,
icon = this.icon,
temp = this.temp,
tempFeelsLike = this.tempFeelsLike,
humidity = this.humidity,
pressure = this.pressure
)
}
@Dao
interface WeatherDao{
@Query("select * from current_weather where id = $WEATHER_ID")
fun getWeather(): LiveData<DatabaseWeather>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(weather: DatabaseWeather)
}
@Database(entities = [WeatherDatabase::class], version = 1)
abstract class WeatherDatabase: RoomDatabase(){
abstract val weatherDao: WeatherDao
}
private lateinit var INSTANCE: WeatherDatabase
fun getDatabase(context: Context): WeatherDatabase{
synchronized(WeatherDatabase::class.java){
if (!::INSTANCE.isInitialized){
INSTANCE = Room.databaseBuilder(context.applicationContext,
WeatherDatabase::class.java, "weather_db").build()
}
}
return INSTANCE
}
Upvotes: 0
Views: 490
Reputation: 12953
You are providing WeatherDatabase
as entity for WeatherDatabase
, instead provide DatabaseWeather
@Database(entities = [DatabaseWeather::class], version = 1)
abstract class WeatherDatabase: RoomDatabase(){
abstract val weatherDao: WeatherDao
}
Upvotes: 1