简体   繁体   English

房间错误:输入时没有可行的选择?

[英]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注释的房间库来更新表,下面是我的代码(在Dao界面中):

@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 ? 我的代码出了什么问题?

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);

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. 错误消息使得感觉Room正在跳过前缀。

This is a bug in Room, at least through 1.0.0-alpha8 . 这是Room中的一个错误,至少通过 1.0.0-alpha8 Track this issue to see when it gets fixed. 跟踪 此问题以查看何时修复。

That's actually not valid SQLite syntax, as it turns out . 事实证明 ,这实际上不是有效的SQLite语法。 Table prefixes go on columns in SELECT statements, not UPDATE statements . 表前缀在SELECT语句中的列上, 而不是在UPDATE语句中

In my case a message was error: no viable alternative at input 'is' (and a query name was mentioned). 在我的情况下,一条消息是error: no viable alternative at input 'is' (并且提到了查询名称)。 I found that the query didn't have necessary spaces between words like here: 我发现查询在这里的单词之间没有必要的空格:

    "from car" +       // Add a space here.
    "where brand_id is null" +

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM