0

I created a complex database view to fetch some data from my tables:

@DatabaseView(viewName = "VIEW_SEDINTE_SPECIALE",
        value = "SELECT descriere AS description, UPPER(titlu) AS upperTitle," +
                "(CASE " +
                "WHEN puncte_premiu * 2 > 100 THEN 100 " +
                "ELSE puncte_premiu * 2 " +
                "END) AS doubledRewards " +
                "FROM sarcini")
public class SpecialTask {
    public String upperTitle;

    public String description;

    public Long doubledRewards;

    public SpecialTask(String upperTitle, String description, Long doubledRewards) {
        this.upperTitle = upperTitle;
        this.description = description;
        this.doubledRewards = doubledRewards;
    }
}

I specified the view to the database, according to the documentation:

@Database(
        entities = {
                Role.class, Task.class, TaskHistory.class, User.class,
                Feedback.class, Location.class, Material.class,
                SessionMaterial.class, SessionTask.class,
                Problem.class, Session.class
        },
        views = {
                SpecialTask.class
        },
        version = 1
)
@TypeConverters(DateConverter.class)
public abstract class PersonalDevelopmentDatabase extends RoomDatabase { ... }

And in my QueriesDao I want to select everything from the View:

@Dao
public interface QueriesDao {
    @Query("SELECT * FROM VIEW_SEDINTE_SPECIALE")
    LiveData<List<SpecialTask>> getSpecialTasks();
}

And I am stuck with the following error:

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: VIEW_SEDINTE_SPECIALE)

I went through many posts and no solution helped me out. How should I solve this ?

1 Answers1

0

If you add a new view or table you have to increment the version of your database. So try and update PersonalDevelopmentDatabase to version 2.

Bohsen
  • 4,242
  • 4
  • 32
  • 58
  • I incremented the version of the database but I still receive the same error. – Andrei Ionescu Jan 14 '20 at 20:48
  • Did you add another DatabaseView somewhere? Maybe in another class? Or maybe changed a query somewhere to reference a Table/View named `VIEW_ROLURI_UTILIZATORI`? – Bohsen Jan 14 '20 at 22:00
  • I have updated my question. I have another view that is called VIEW_ROLURI_UTILIZATORI but I get the same error for both of them. And to make the problem more visible I commented the other error and forgot to edit. – Andrei Ionescu Jan 14 '20 at 22:27
  • I believe your `DatabaseView` is missing a constructor and also drop the `\n` from the queries. I don't think they're needed. – Bohsen Jan 14 '20 at 22:37
  • I defined a public constructor, I get the same result. The `\n` are useful as a delimiter between the words. I replaced them with spaces for clarity. – Andrei Ionescu Jan 14 '20 at 22:45