Android Room SQLite_ERROR没有这样的表 [英] Android Room SQLite_ERROR no such table

查看:255
本文介绍了Android Room SQLite_ERROR没有这样的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Android Room 及之后的版本跟随本教程,当我尝试构建应用程序时遇到以下错误:

I'm trying my hand at using Android Room and after following this tutorial I'm getting the following error when i try to build the app:

Error:(23, 27) error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: screen_items)

该名称很好,应该存在.进行更改后,我清理了该项目,并确保已将其从设备上完全卸载.

The name is fine and should exist. After making my changes I cleaned the project and made sure it was completely uninstalled from the device.

在我的Activity中,我使用以下行来初始化onCreate中的内容:

In my Activity I'm initialising the things in onCreate with this line:

db = AppDatabase.getDatabase(getApplicationContext());

这是我的代码:

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;
  }
}

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

推荐答案

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

Room names tables the same as their associated entities. In your DAO, TABLE_ITEMS needs to be PermitItem, because your entity is PermitItem. Or, add the tableName property to the @Entity annotation, to tell Room some other name to use for the table.

这篇关于Android Room SQLite_ERROR没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆