Answer the question
In order to leave comments, you need to log in
The code using room. What is the problem?
I recently learned about such a wonderful library as room. I decided to use it in my new project and nothing works for me, it gives an error. I've been looking for 10 differences between my code and other people's code for a long time and I can't figure out what the reason is. Here is the code:
ProductDao interface:
@Dao
interface ProductDao {
@Insert
fun addProduct(product: Product)
@Update
fun updateProduct()
@Delete
fun deleteProduct(name: String)
@Query("SELECT * FROM Vegetables")
fun getProduct()
}
@Entity
data class Vegetables(val name: String,
val about: String,
var price: String
)
@Database(entities = arrayOf(Vegetables::class), version = 1, exportSchema = false)
abstract class ProductDatabase : RoomDatabase() {
abstract fun productDao(): ProductDao
companion object {
@Volatile
private var INSTANCE: ProductDatabase? = null
fun getDatabase(context: Context): ProductDatabase {
if (INSTANCE != null) {
return INSTANCE!!
}
synchronized(this) {
INSTANCE = Room.databaseBuilder(context, ProductDatabase::class.java, "database")
.fallbackToDestructiveMigration().build() //Вот тут возникает ошибка
return INSTANCE!!
}
}
}
}
var db = ProductDatabase.getDatabase(this)
dependencies {
implementation("[androidx.room](http://androidx.room):room-runtime:2.3.0")
annotationProcessor("[androidx.room](http://androidx.room):room-compiler:2.3.0")
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question