gpt4 book ai didi

java - Android架构组件: Room : No such table

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:10 25 4
gpt4 key购买 nike

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

“错误:(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com