Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room error: no viable alternative at input?

I am trying to update the table using @Query annotation of room library, below is my code ( In Dao interface ) :

@Query("UPDATE table_name SET table_name.col1 = :val1 WHERE table_name.col2 = :val2")
void updateValue(long val1, long val2);

Complete error string as below :

Error:(11, 10) error: no viable alternative at input 'UPDATE table_name SET table_name.'

Here is entity class :

@Entity(tableName = "table_name")
public class SampleTable {

    @PrimaryKey
    @ColumnInfo(name = "_id")
    private Long Id;

    @ColumnInfo(name = "col1")
    private Long column1;

    @ColumnInfo(name = "col2")
    private Long column2;

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

    public Long getColumn1() {
        return column1;
    }

    public void setColumn1(Long column1) {
        this.column1 = column1;
    }

    public Long getColumn2() {
        return column2;
    }

    public void setColumn2(Long column2) {
        this.column2 = column2;
    }
}

What's wrong with my code ?

like image 227
Prashant Avatar asked Aug 08 '17 18:08

Prashant


2 Answers

This error could also happen in this case:

If you are passing a list, don't forget to add the parentheses enclosing your list variable, otherwise you will get the same error:

@Query("select name from mytable where name in (:myList)")
LiveData<List<String>> findNames(List<String> myList);
like image 64
live-love Avatar answered Oct 18 '22 08:10

live-love


Try changing your statement to:

UPDATE table_name SET col1 = :val1 WHERE col2 = :val2.

The error message makes it feel like Room is tripping over the prefix.

This is a bug in Room, at least through 1.0.0-alpha8. Track this issue to see when it gets fixed.

That's actually not valid SQLite syntax, as it turns out. Table prefixes go on columns in SELECT statements, not UPDATE statements.

like image 6
CommonsWare Avatar answered Oct 18 '22 09:10

CommonsWare