Android Room Sqlite没有这样的列比在WHERE条件下使用等号(=)

问题描述 投票:0回答:1

这有效:

@Query("SELECT *FROM users " + " WHERE name1 LIKE :blabla") 
List<User> findUsersByName(String blabla);

这不起作用:

@Query("SELECT *FROM users " + " WHERE name1=blabla")
List<User> findUsersByName(String blabla);

并带来此错误消息:

“错误:(28,16)错误:查询有问题:[SQLITE_ERROR] SQL错误或缺少数据库(没有这样的列:blabla)”

我很感谢任何帮助。

这是我的完整代码:

user DAO.Java:

@Dao
public interface UserDao {
@Insert
void insert(User user);

@Query("SELECT *FROM users " + " WHERE name1 = blabla")
List<User> findUsersByName(String blabla);

@Query("SELECT *FROM users")
List<User> getAllUsers();

@Query("SELECT COUNT(user_id) FROM users")
int getNumberOfUsers();

@Delete
void delete(User user);
}

my database.Java:

@Database(entities = {User.class}, version = 1)
public abstract class MyDatabase extends RoomDatabase{
public abstract UserDao getUserDao();
}

user.Java

@Entity(tableName = "users")
public class User {

@PrimaryKey
@ColumnInfo(name = "user_id")
private int id;

@ColumnInfo(name = "name1")
private String name;

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public User(int id, String name){
    this.id=id;
    this.name=name;

}

}
java android sqlite
1个回答
1
投票

更换:

@Query("SELECT *FROM users " + " WHERE name1 = blabla")
List<User> findUsersByName(String blabla);

有:

@Query("SELECT *FROM users " + " WHERE name1 = :blabla")
List<User> findUsersByName(String blabla);

我,你错过了冒号。

© www.soinside.com 2019 - 2024. All rights reserved.