我正在尝试使用新的架构组件,但是当我尝试运行时,我得到:
“错误:(375, 24) 错误:查询有问题:[SQLITE_ERROR] SQL 错误或缺少数据库(没有这样的表:帖子)”
以下是我的类(class)。
**实体:**
@Entity
public static class Post {
@PrimaryKey
private String id;
@ColumnInfo(name = "data")
private String data;
public String getId() {
return id;
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return data;
}
public void setId(String id) {
this.id = id;
}
}
DAO:
@Dao
public interface PostDao {
@Query("SELECT * FROM posts")
LiveData<List<Post>> getAll();
@Insert
void insertAll(Post... posts);
@Insert
void insert(Post post);
@Delete
void delete(Post post);
}
数据库:
@Database(entities = {Post.class}, version = 1)
public static abstract class AppDatabase extends RoomDatabase {
public abstract PostDao postDao();
}
最佳答案
By default, Room uses the class name as the database table name. If you want the table to have a different name, set the tableName property of the @Entity annotation, as shown in the following code snippet:
https://developer.android.com/topic/libraries/architecture/room.html
您似乎认为它会自行使类复数化。
所以,要么使用 SELECT * FROM Post
或者做
@Entity(tableName = "posts")
class Post {
...
}
关于java - Android架构组件: Room : No such table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44602312/