【问题标题】:Android Room SQLite_ERROR no such tableAndroid Room SQLite_ERROR 没有这样的表
【发布时间】:2018-01-17 13:33:31
【问题描述】:

我正在尝试使用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;
  }
}

PermitItem

@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 + '\'' +
            '}';
  }


}

PermitItemDao

@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();
}

【问题讨论】:

  • 为什么SQLiteHandler 存在?定义 screen_items 的 Room 实体在哪里? AppDatabaseSQLiteHandler 是否都在尝试使用同一个数据库文件?
  • 嗯,它没有,因为教程中没有,但我回想一下,我可能需要它来创建表格
  • Room 创建表格,作为设置和使用AppDatabase 的一部分。您尝试访问 screen_items 的代码在哪里?
  • 啊,很公平,我现在已经完全删除了它,问题仍然存在。它是每个@Query 标签。我在PermitItemDao 中得到了每个错误日志

标签: android sqlite dao android-room


【解决方案1】:

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

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

确保数据库文件夹中有最新的 db 文件,如果导出架构,请确保 app\schemas 下的 .json 架构文件正确更新。

【讨论】:

  • 这对我来说是有效的;我没有将类添加到实体列表中;容易错过
【解决方案2】:

房间名称表与其关联的实体相同。在您的 DAO 中,TABLE_ITEMS 必须是 PermitItem,因为您的实体是 PermitItem。或者,将 tableName 属性添加到 @Entity 注释中,以告诉 Room 为该表使用的其他名称。

【讨论】:

  • 谢谢!现在运行良好..我的主键也设置为 int 但只有 long 有效
  • 非常感谢您参考tableName属性
【解决方案3】:

看文章对我有很大帮助。 我有这个 errorDatabase(entities = {Folder.class}, version = 1, exportSchema = false)

只需添加我的其他课程 @Database(entities = {Folder.class, Urls.class}, 版本 = 1, exportSchema = false)

【讨论】:

    【解决方案4】:

    如果您刚刚添加了一个新表,只需更新您的数据库类(即扩展 RoomDatabase() 类的类)并更新实体注释

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

    希望它可以节省您寻找答案的时间,愉快的编码。

    【讨论】:

      【解决方案5】:

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

      @Query("SELECT * FROM tablename")
      

      检查您的表名,确保它们与模型类中注释的表名匹配。

      我希望这会有所帮助。编码愉快!

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 2020-10-17
          • 1970-01-01
          • 1970-01-01
          • 2018-12-20
          • 2019-03-11
          • 1970-01-01
          • 1970-01-01
          • 2023-01-24
          • 1970-01-01
          相关资源
          最近更新 更多