vijay raghavan
vijay raghavan

Reputation: 48

no such table: TableName (code 1): , while compiling: SELECT * FROM TableName

I am trying to delete the table using room Database and then executing Select Query on my table.

The problem is that one time it's executing the query properly. But another time it's throwing the below exception

Fatal Exception: android.database.sqlite.SQLiteException: no such table: TableName (code 1): , while compiling: SELECT * FROM TableName

Below is the code to Delete Table

    @Query("DELETE FROM TableName")
    public void delete_table();

Select Query

    @Query("SELECT * FROM TableName")
    public LiveData<List<Table>> getAllValues();

Model class for that table

@Entity(tableName = "TableName")
public class Table {


    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "LID")
    private int id;

    @ColumnInfo(name = "LOC_NAME")
    private String loc_name;

    public Table(){

    }

    public Table(String loc_name) {
        this.loc_name = loc_name;
    }

  public int getId() {
        return id;
    }

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

    public String getLoc_name() {
        return loc_name;
    }

    public void setLoc_name(String loc_name) {
        this.loc_name = loc_name;
    }
}

Room Database Class

@Database(entities = {Table.class}, version = 2)
public abstract class RoomDB extends RoomDatabase {

    public static final String DATABASE_NAME = "db-name";

    
    public abstract DataBaseDao getDatabaseDao();
    private static RoomDB instance;


    public static DataBaseDao getInstance(Context context){
        if (instance == null){
            synchronized (GoBumprRoomDB.class){
                if (instance==null){
                    instance = Room.databaseBuilder(context, RoomDB.class, DATABASE_NAME).fallbackToDestructiveMigration().build();
                }
            }
        }
        return instance;
    }

}

DAO class

@Dao
public interface DataBaseDao {

    @Query("SELECT * FROM TableName")
    public LiveData<List<Table>> getAllValues();

    @Query("DELETE FROM TableName")
    public void delete_table();

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    public void insertIntoTable(List<Table> tableList);
}

I am getting values from Rest call and then storing it on room DB

    public void addListOfTable(List<Table> tables) {
        
        dao.insertIntoTable(tables);

    }


    public void deleteTable() {
        
         dao.deleteTable();
           
    }

Please help me how to fix this issue.

I am using Room DataBase

Upvotes: 0

Views: 507

Answers (1)

Taki
Taki

Reputation: 3730

I guess the problem is that you re passing your room db class to your database instead of the entity itself , to fix that you need to do the following

@Database(entities = {Table.class}, version = 2) /// put Table.class instead

Upvotes: 0

Related Questions