Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android architecture components : Room : No such table

I'm trying to use the new Architecture components, but when I try to run, I get :

"Error:(375, 24) error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: posts)"

The following are my classes.

**ENTITY : **

@Entity
    public static class Post {
        @PrimaryKey
        private String id;

        @ColumnInfo(name = "data")
        private String data;

        public String getId() {
            return id;
        }

        public void setData(String data) {
            this.data = data;
        }

        public String getData() {
            return data;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

DAO :

    @Dao
    public interface PostDao {
        @Query("SELECT * FROM posts")
        LiveData<List<Post>> getAll();

        @Insert
        void insertAll(Post... posts);

        @Insert
        void insert(Post post);

        @Delete
        void delete(Post post);
    }

The database :

@Database(entities = {Post.class}, version = 1)
    public static abstract class AppDatabase extends RoomDatabase {
        public abstract PostDao postDao();
    }
like image 324
Relm Avatar asked Jun 17 '17 08:06

Relm


1 Answers

By default, Room uses the class name as the database table name. If you want the table to have a different name, set the tableName property of the @Entity annotation, as shown in the following code snippet:

https://developer.android.com/topic/libraries/architecture/room.html

It seems you assumed it would pluralize the class on its own.

So, either use SELECT * FROM Post

or do

@Entity(tableName = "posts")
class Post {
    ...
}
like image 190
OneCricketeer Avatar answered Nov 15 '22 13:11

OneCricketeer