Android 架构组件:房间:没有这样的 table

Android architecture components : Room : No such table

我正在尝试使用新的架构组件,但是当我尝试 运行 时,我得到:

"Error:(375, 24) error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: posts)"

以下是我的类.

**实体:**

@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

您似乎认为它会自行复数 class。

所以,要么使用 SELECT * FROM Post

或者做

@Entity(tableName = "posts")
class Post {
    ...
}