Android Room SQLite_ERROR没有这样的表

jam*_*z77 23 sqlite android dao android-room

我正在尝试使用Android Room,在完成本教程之后,当我尝试构建应用程序时,我收到以下错误:

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());
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

AppDatabase

@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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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


}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

liv*_*ove 70

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

    @Database(entities = {XEntity.class, YEntity.class, ZEntity.class}, 
version = 1, exportSchema = true)
Run Code Online (Sandbox Code Playgroud)

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

  • 这对我来说是有效的;我没有将类添加到实体列表中;容易错过 (12认同)

Com*_*are 25

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


yil*_*llo 11

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

只需添加我的另一个类 @Database(entities = {Folder.class, Urls.class}, version = 1, exportSchema = false)


ral*_*abb 6

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

@Database(entities = [User::class, NewTableHere::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
Run Code Online (Sandbox Code Playgroud)

希望它可以节省您搜索答案的时间,快乐编码。