2017-08-09 2 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'を定義するRoomエンティティはどこですか? 'AppDatabase'と' SQLiteHandler'は同じデータベースファイルで動作しようとしていますか? – CommonsWare

+0

チュートリアルに1つもなかったので、テーブルを作成するために必要かもしれないと考えて戻しました – jampez77

+0

ルームはテーブルを作成し、 AppDatabase'。 'screen_items'にアクセスしようとしているコードはどこですか? – CommonsWare

答えて

10

ルーム名テーブル。あなたのDAOではPermitItemであるため、PermitItemにする必要があります。または、tableNameプロパティを@Entityアノテーションに追加して、ルームにテーブルに使用する別の名前を教えてください。

+0

ありがとう!これでうまくいきました。私のプライマリキーは 'int'に設定されていましたが、' long'だけが動作します。 – jampez77

+0

tableNameプロパティを参照していただきありがとうございます –

1

AppDatabase.javaファイルに記載されていない実体可能性があり、このエラーのもう一つの理由:あなたのスキーマをエクスポートする場合

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

あなたは、データベースフォルダ内の最新のdbファイルがあることを確認してください、そして、 app \ schemasの下にある.jsonスキーマファイルが正しく更新されていることを確認してください。

関連する問題