Search code examples
androiddatabaseandroid-studioandroid-sqliteandroid-room

Failed to create table with @Query in Room database


My @Dao interface is:

@Dao
public interface TaskDao {
    @Insert
    void insert(Task task);

    @Update
    void update(Task task);

    @Delete
    void delete(Task task);

    @Query("DELETE FROM task_table")
    void deleteAllTasks();

    @Query("SELECT * FROM task_table")
    LiveData<List<Task>> getAllMainTasks();
}

Using this query every thing works fine and just as wanted. Here is the table screenshot: enter image description here

Changing the last two lines to below lines causing problem:

    @Query("SELECT * FROM task_table WHERE creator_ID = NULL ORDER BY task_ID ASC")
    LiveData<List<Task>> getAllMainTasks();

But when I use this, no entry in this table is created!


Solution

  • Try to replace your null-condition:

    @Query("SELECT * FROM task_table WHERE creator_ID = NULL ORDER BY task_ID ASC")
    

    to

    @Query("SELECT * FROM task_table WHERE creator_ID is NULL ORDER BY task_ID ASC")