androidkotlinandroid-room

[Android][Room- database] error: [SQLITE_ERROR] SQL error or missing database (near "missing": syntax error)


I am using Room database for saving data. I have two api and i need to fetch data from cloud and save local db using room. First api was users list and that is properly fetching from cloud and saving into database(Room). Second api was fetching ShopDetails. For that I Created below classes:

ShopDetailsEntity.kt

@Entity(tableName = AppConstants.SHOP_DETAILS)
data class ShopDetailsEntity(
    @PrimaryKey @ColumnInfo(name = AppConstants.COLUMN_SHOP_ID) var shopId: Long,
    @ColumnInfo(name = AppConstants.COLUMN_COMPANY_ID) val companyId: Long,
    @ColumnInfo(name = AppConstants.COLUMN_COMPANY_NAME) val companyName: String,
)

BaseDao.kt

@Dao
interface BaseDao<T> {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(data: T)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(data: List<T>)

    @Update
    suspend fun update(data: T)

    @Delete
    suspend fun delete(data: T)
}

IShopDetailDao.kt

@Dao
interface IShopDetailDao: BaseDao<ShopDetailsEntity> {

    @Query("SELECT * FROM ${AppConstants.SHOP_DETAILS}")
    suspend fun getAllShopDetails(): List<ShopDetailsEntity>

}

AppDatabase.kt

@Database(
    entities = [UserEntity::class],[ShopDetailsEntity::class],
    version = AppConstants.DB_VERSION,
    exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userProfileDao(): IUserProfileDao
    abstract fun shopDetailDao(): IShopDetailDao

    companion object {
        @Volatile
        private var instance: AppDatabase? = null


        fun getDatabase(context: Context): AppDatabase =
            instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }

        private fun buildDatabase(context: Context) =
            Room.databaseBuilder(
                context.applicationContext, AppDatabase::class.java,
                AppConstants.DB_NAME
            )
                .fallbackToDestructiveMigration()
                .build()
    }

}

When I am running I am getting this error:

CollectionApp\app\build\tmp\kapt3\stubs\debug\com\collectionapp\main\model\dto\ShopDetailsEntity.java:10: error: View class must be annotated with @DatabaseView public final class ShopDetailsEntity {

I have used User details api for saving to Room DB , it is working fine, in the same way I used to save shop details.But it is showing above error.


Solution

  • The problem lies here:

    @Database(
        entities = [UserEntity::class],[ShopDetailsEntity::class],
        version = AppConstants.DB_VERSION,
        exportSchema = false
    )
    

    It becomes more clear when properly formatted with added parameter names:

    @Database(
        entities = [UserEntity::class],
        views = [ShopDetailsEntity::class],
        version = AppConstants.DB_VERSION,
        exportSchema = false,
    )
    

    You actually supply ShopDetailsEntity::class as a view, not an entity.

    What you really want is to add it to the array of entities:

    @Database(
        entities = [UserEntity::class, ShopDetailsEntity::class],
        version = AppConstants.DB_VERSION,
        exportSchema = false,
    )