Home > Software engineering >  Getting Room database "Invalid query argument" error while building project
Getting Room database "Invalid query argument" error while building project

Time:11-05

I am using Room Database to store data in my application. But, trying to compile the project, I get the following error: "Invalid query argument". I tried to find the reason in the official documentation and also googling cause of this error, but it was all to no avail. Please, could you help by explaining what this error is in general, what causes it and what I need to fix in the project?

Exact error message

C:\Users\***\ParsedDatabase.java:10: error: Invalid query argument: int. It must be a class or an interface.
public final class ParsedDatabase {
             ^

Model - ParsedDatabase (Room Database entity, class with problem)

@Entity(tableName = "parsed_database")
data class ParsedDatabase(
    @PrimaryKey()
    val id: Int = 1,
    @ColumnInfo(name = "app_config")
    val appConfigSection: HashMap<String, String>,
    @ColumnInfo(name = "loans")
    val loansList: ArrayList<Offer>,
    @ColumnInfo(name = "cards")
    val cardsList: ArrayList<ArrayList<Offer>>,
    @ColumnInfo(name = "credits")
    val creditsList: ArrayList<Offer>,
) {

    enum class AppConfigKeys {
        app_config,
        user_term_html,
        extra_field_,
    }

    enum class LoansAndCreditsKeys {
        loans,
        credits,
    }

    enum class CardsKeys {
        cards,
    }

}

Dao class (I attach just in case)

@Dao
interface LocalDatabaseDao {
    @Insert(onConflict = REPLACE)
    fun save(parsedDatabase: ParsedDatabase)

    @Query("SELECT * FROM parsed_database WHERE id = :id")
    fun load(id: Int = 1): ParsedDatabase?

    @Delete
    fun delete(parsedDatabase: ParsedDatabase)

    @Delete(entity = ParsedDatabase::class)
    fun delete(id: Int = 1)
}

Database class

@Database(entities = [ParsedDatabase::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class LocalDatabase : RoomDatabase() {
    abstract fun getLocalDatabaseDao(): LocalDatabaseDao
}

CodePudding user response:

    @Delete(entity = ParsedDatabase::class)
    fun delete(id: Int = 1)

I do not think that Room knows what to do with this. If you need that operation, you could try:

    @Query("DELETE FROM parsed_database WHERE id = :id")
    fun delete(id: Int = 1)
  • Related