2017-08-09 43 views
3

我想我的手在使用Android Room及以下this tutorial後,我收到以下錯誤,當我嘗試構建應用程序:安卓房SQLite_ERROR沒有這樣的表

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

這裏是我的代碼:

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

爲什麼'SQLiteHandler'存在?你的房間實體在哪裏定義'screen_items'? 'AppDatabase'和'SQLiteHandler'都試圖使用同一個數據庫文件嗎? – CommonsWare

+0

那麼它並沒有在教程中沒有,但我認爲我可能需要它來創建表格(S) – jampez77

+0

Room創建表格,作爲設置和使用' AppDatabase'。你試圖訪問'screen_items'的代碼在哪裏? – CommonsWare

回答

10

間名錶一樣有聯繫實體。在您的DAO中,TABLE_ITEMS需要爲PermitItem,因爲您的實體是PermitItem。或者,將tableName屬性添加到@Entity註釋中,以告知Room用於該表的其他名稱。

+0

謝謝!這現在工作得很好..我也有我的主鍵設置爲'int',但只有'長'工作 – jampez77

+0

非常感謝您指的tableName屬性 –

1

另一個原因錯誤可能是企業未在AppDatabase.java文件中列出:

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

請確保您有在數據庫文件夾中的最新的數據庫文件,如果您導出模式,確保app \ schemas下的.json模式文件正在正確更新。