DEV Community

Cover image for Room database. DOAs
Tristan Elliott
Tristan Elliott

Posted on • Updated on

Room database. DOAs

Introduction

  • This series is going to be dedicated to the basics of an SQLite database. I will be following the official google guide, HERE but I will be working in an order that makes more sense to me.

Youtube Version

HERE

What is a DAO?

  • Great question! When we are using the Room persistence library to store our app's data, we use data access objects (DAO) to interact with the data.

  • Each DAO will include methods that offer abstract access to our app's database. At compile time Room will automatically generate the implementation of the DAOs that we define.

  • By using a DAO instead of a direct SQLite query we create a much cleaner and testable code base.

Defining a DAO

  • When creating an DAO we must define it as either an interface or an abstract class. However for simple examples like we are using it is recommended that we use an interface.
@Dao
public interface WordDAO {

    @Insert
    void insert(Word word);

    @Query("DELETE FROM word_table")
    void deleteAll();

    @Query("SELECT * FROM word_table")
    List<Word> getAllWords();
}

Enter fullscreen mode Exit fullscreen mode

@Dao

  • In order to create a DAO we must annotate our interface with a @Dao. This is what tells the the Room library that this class represents a data access object.

  • When we create a DAO there are two types of methods that are used to define database operations.

1) Convivence Methods : These are basic operations that have been defined as annotations for us. @Insert, @Update, @Delete.

2) Query Methods : Methods marked with @Query and let us write our own SQL queries to interact with the database.

@Insert

  • This marks a method in a @Dao annotated class as an insert method. When Room implements this method it will insert its parameters into the database. All of the parameters of the insert method must be either a class annotated with @Entity or a collection/array of annotated classes. Notice how our parameter is a word class that is annotated with @Entity

@Query("DELETE FROM word_table")

  • As stated earlier this is a query method and we are using it to execute an SQL statement. As you can probably guess this is used to delete all the rows from our table called word_table but lets dive a little deeper into the SQL. When trying to delete something from a table in SQL we use the DELETE clause and the syntax typically goes like this:
DELETE FROM table_name WHERE condition
Enter fullscreen mode Exit fullscreen mode
  • all the SQL clauses are in uppercase

  • Now in our query we did not specify a condition so that means that our sql statement will delete all the rows inside of our word_table.

@Query("SELECT * FROM word_table")

  • Here we are using the SELECT clause in combination with the FROM clause and it is used to define which table we want to search . The * indicates all the columns and rows. Loosely translated to human speech this statement reads:

Show me all the columns and all the rows in the word_table

  • The last thing I want to point out is the return type for the getAllWords() method. Currently it has a return type of List and it uses Generics to make sure that at runtime our list contains Word objects. In Future blog posts this return type will be changed to LiveData.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)