Android房间SQLite_ERROR没有这样的表

3pmvbmvn  于 2022-12-13  发布在  SQLite
关注(0)|答案(7)|浏览(355)

我正在尝试使用Android Room,在遵循this tutorial之后,当我尝试构建应用程序时,我遇到了以下错误:
Error:(23, 27) error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: screen_items)
名称很好,应该存在。在做了我的更改后,我清理了项目,并确保它完全从设备中卸载。
在我的Activity中,我用下面这行初始化onCreate中的内容:

db = AppDatabase.getDatabase(getApplicationContext());

下面是我的代码:

应用程序数据库

@Database(entities = {PermitItem.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
  public static String DATABASE_NAME = "my_database";
  public final static String TABLE_ITEMS = "screen_items";

  private static AppDatabase INSTANCE;

  public abstract PermitItemDao permitItemModel();

  public static AppDatabase getDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE = Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME).allowMainThreadQueries().build();
    }
    return INSTANCE;
  }

  public static void destroyInstance() {
    INSTANCE = null;
  }
}

许可证项目

@Entity
public class PermitItem {
  @PrimaryKey(autoGenerate = true)
  public final int id;
  private String posX, posY, width, height, content, type;

  public PermitItem(int id, String posX, String posY, String width, String height, String content, String type) {
    this.id = id;
    this.posX = posX;
    this.posY = posY;
    this.width = width;
    this.height = height;
    this.content = content;
    this.type = type;
  }

  public static PermitItemBuilder builder(){
    return new PermitItemBuilder();
  }

  public static class PermitItemBuilder{
    int id;
    String posX, posY, width, height, content, type;

    public PermitItemBuilder setId(int id) {
        this.id = id;
        return this;
    }

    public PermitItemBuilder setPosX(String posX) {
        this.posX = posX;
        return this;
    }

    public PermitItemBuilder setPosY(String posY) {
        this.posY = posY;
        return this;
    }

    public PermitItemBuilder setWidth(String width) {
        this.width = width;
        return this;
    }

    public PermitItemBuilder setHeight(String height) {
        this.height = height;
        return this;
    }

    public PermitItemBuilder setContent(String content) {
        this.content = content;
        return this;
    }

    public PermitItemBuilder setType(String type) {
        this.type = type;
        return this;
    }

    public PermitItem build() {
        return new PermitItem(id, posX, posY, width, height, content, type);
    }
  }

  public long getId() {
    return id;
  }

  public String getPosX() {
    return posX;
  }

  public void setPosX(String posX) {
    this.posX = posX;
  }

  public String getPosY() {
    return posY;
  }

  public void setPosY(String posY) {
    this.posY = posY;
  }

  public String getWidth() {
    return width;
  }

  public void setWidth(String width) {
    this.width = width;
  }

  public String getHeight() {
    return height;
  }

  public void setHeight(String height) {
    this.height = height;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  @Override
  public String toString() {
    return "PermitItem{" +
            "id=" + id +
            ", posX='" + posX + '\'' +
            ", posY='" + posY + '\'' +
            ", width='" + width + '\'' +
            ", height='" + height + '\'' +
            ", content='" + content + '\'' +
            ", type='" + type + '\'' +
            '}';
  }

}

许可证项道

@Dao
public interface PermitItemDao {

  @Insert(onConflict = OnConflictStrategy.REPLACE)
  long addPermitItem(PermitItem permitItem);

  @Query("select * from " + TABLE_ITEMS)
  ArrayList<PermitItem> getAllPermitItems();

  @Query("select * from " + TABLE_ITEMS + " where id = :id")
  PermitItem getPermitItemById(int id);

  @Update(onConflict = OnConflictStrategy.REPLACE)
  void updatePermitItem(PermitItem permitItem);

  @Query("delete from " + TABLE_ITEMS)
  void removeAllPermitItems();
}
tzdcorbm

tzdcorbm1#

出现此错误的另一个原因可能是该实体未在www.example.com文件中列出AppDatabase.java:

@Database(entities = {XEntity.class, YEntity.class, ZEntity.class}, 
version = 1, exportSchema = true)

确保databases文件夹中有最新的db文件,如果导出模式,请确保app\schemas下的.json模式文件已正确更新。

wqsoz72f

wqsoz72f2#

Room名称表格与其相关的实体相同。在您的DAO中,TABLE_ITEMS必须是PermitItem,因为您的实体是PermitItem。或者,将tableName属性加入@Entity注解,告诉Room表格使用其他名称。

uttx8gqw

uttx8gqw3#

看到这些文章对我帮助很大。我有这个错误数据库(实体= {Folder.class},版本= 1,exportSchema = false)
只需添加我的另一个类@数据库(实体= {文件夹.类,URL.类},版本= 1,导出架构= false)

ny6fqffe

ny6fqffe4#

如果您刚刚添加了一个新表,只需更新Database类(扩展RoomDatabase()类的一个类)并更新实体注解

@Database(entities = [User::class, NewTableHere::class], version = 1)
abstract class AppDatabase : RoomDatabase() {

希望它能节省你寻找答案的时间,快乐的编码。

x33g5p2x

x33g5p2x5#

在我的例子中,问题的原因是使用了错误的表名。我为模型类选择的表名与类名不同。我在查询Data-Access-Object接口时错误地使用了类名。

@Query("SELECT * FROM tablename")

检查您的表名,确保它们与注解到模型类中的表名匹配。
希望这能帮上忙,编码快乐!

1sbrub3j

1sbrub3j6#

我问题是TABLE NAME:|||我把我的表名写错了:(((

fdx2calv

fdx2calv7#

在我的例子中,这是因为没有将实体类添加到数据库文件的数据库装饰器上的实体中

相关问题