76

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.

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

db = AppDatabase.getDatabase(getApplicationContext());

Here is my code:

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();
}
4
  • 1
    Why does SQLiteHandler exist? Where is your Room entity that defines screen_items? Are both AppDatabase and SQLiteHandler trying to work with the same database file? Commented Aug 9, 2017 at 15:10
  • Well it didn't as there wasn't one in the tutorial but i put it back in thinking that i might need it in order to create the table(s) Commented Aug 9, 2017 at 15:11
  • Room creates the tables, as part of setting up and using AppDatabase. Where is your code that is attempting to access screen_items? Commented Aug 9, 2017 at 15:12
  • Ahh fair enough, well i have removed it completely now and the issue still persists. It's each of the @Query tags. I get an error log for each one in PermitItemDao Commented Aug 9, 2017 at 15:17

7 Answers 7

224

Another reason for this error could be the entity is not listed in the AppDatabase.java file:

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

Make sure you have the latest db file in the databases folder, and if you export your schema, make sure your .json schema file under app\schemas is being properly updated.

Sign up to request clarification or add additional context in comments.

2 Comments

this was what worked in my case; I had not added the classes to the entity list; easy to miss
I had a similar issue and this answer resolved it. I am surprised that a powerful tool like Android Studio cannot provide a bit more helpful error messages.
51

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.

3 Comments

Thank you! That's working well now..I also had my Primary key set as int but only long works
Thank you very much for referring to tableName property
4 years passed and this answer still saved my day Thanks!
18

If you just added a new table, just update your Database class (That one class extending the RoomDatabase() class) and update the entities annotation

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

Wish it saves you time on searching for answer, happy coding.

Comments

15

It helped me a lot to see the articles. I had this errorDatabase(entities = {Folder.class}, version = 1, exportSchema = false)

just add my other class @Database(entities = {Folder.class, Urls.class}, version = 1, exportSchema = false)

Comments

2

In my case, the cause of the problem was using the wrong table name. The table name I chose for my model class was different from the class name. I erroneously used the class name while querying my Data-Access-Object interface.

@Query("SELECT * FROM tablename")

Check your table name to make sure they match with the table name annotated into the model class.

I hope this helps. Merry coding!

Comments

1

my problem was with TABLE NAME :||| I wrote my table name In the wrong word :(((

Comments

1

In my case it was because of not adding the entity class to entities on the Database decorator on the Database file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.