When you use the Room persistence library to store your app's data, you interact with the stored data by defining data access objects, or DAOs. Each DAO includes methods that offer abstract access to your app's database. At compile time, Room automatically generates implementations of the DAOs that you define. Show
By using DAOs to access your app's database instead of query builders or direct queries, you can preserve separation of concerns, a critical architectural principle. DAOs also make it easier for you to mock database access when you test your app. Anatomy of a DAOYou can define each DAO as either an interface or an abstract class. For basic use cases,
you should usually use an interface. In either case, you must always annotate your DAOs with The following code is an example of a simple DAO that defines methods for inserting, deleting, and selecting
There are two types of DAO methods that define database interactions:
The following sections demonstrate how to use both types of DAO methods to define the database interactions that your app needs. Convenience methodsRoom provides convenience annotations for defining methods that perform simple inserts, updates, and deletes without requiring you to write a SQL statement. If you need to define more complex inserts, updates, or deletes, or if you need to query the data in the database, use a query method instead. InsertThe
Each parameter for an If the UpdateThe
Room uses the primary key to match passed entity instances to rows in the database. If there is no row with the same primary key, Room makes no changes. An DeleteThe
Room uses the primary key to match passed entity instances to rows in the database. If there is no row with the same primary key, Room makes no changes. A Query methodsThe Room validates SQL queries at compile time. This means that if there's a problem with your query, a compilation error occurs instead of a runtime failure. Simple queriesThe following code defines a method that uses a simple
The following sections demonstrate how to modify this example for typical use cases. Return a subset of a table's columnsMost of the time, you only need to return a subset of the columns from the table that you are querying. For example, your UI might display just the first and last name for a user instead of every detail about that user. In order to save resources and streamline your query's execution, you should only query the fields that you need. Room allows you to return a simple object from any of your queries as long as you can map the set of result columns onto the returned object. For example, you can define the following object to hold a user's first and last name:
Then, you can return that simple object from your query method:
Room understands that the query returns values for the Pass simple parameters to a queryMost of the time, your DAO methods need to accept parameters so that they can perform filtering operations. Room supports using method parameters as bind parameters in your queries. For example, the following code defines a method that returns all of the users above a certain age:
You can also pass multiple parameters or reference the same parameter multiple times in a query, as demonstrated in the following code:
Pass a collection of parameters to a querySome of your DAO methods might require you to pass in a variable number of parameters that is not known until runtime. Room understands when a parameter represents a collection and automatically expands it at runtime based on the number of parameters provided. For example, the following code defines a method that returns information about all of the users from a subset of regions:
Query multiple tablesSome of your queries might require access to
multiple tables to calculate the result. You can use The following code defines a method that joins three tables together to return the books that are currently on loan to a specific user:
You can also define simple objects to return a subset of columns from multiple joined tables as discussed in Return a subset of a table's columns. The following code defines a DAO with a method that returns the names of users and the names of the books that they have borrowed:
Return a multimapIn Room 2.4 and higher, you can also query columns from multiple tables without defining an additional data class by writing query methods that return a multimap. Consider the example from the Query multiple tables section. Instead of returning a list
of instances of a custom data class that holds pairings of
When your query method returns a multimap, you can write queries that use
If you don't need to map entire objects, you can also return mappings between specific columns in your query by setting the
Special return typesRoom provides some special return types for integration with other API libraries. Paginated queries with the Paging libraryRoom supports paginated queries through integration with the
Paging library. In
For more information about choosing type parameters for a Direct cursor accessIf your app's logic requires direct access to the return rows, you can write your DAO methods to return a
Additional resourcesTo learn more about accessing data using Room DAOs, see the following additional resources: Samples
Codelabs
Which of the following is used to find data in a database?Queries. Queries can perform many different functions in a database. Their most common function is to retrieve specific data from the tables.
Which of the following is a database object you can use to enter edit or display data from a table or query?A form is an Access object with which you can enter, edit, or display data from a table or query. Using an object's Property Sheet, you can make precise changes to the object's characteristics.
Which Access object contains all of the data in the database quizlet?Tables contain all of the raw data in the database. Access information can be printed from forms, queries, and tables.
On which tab would you find Access's features for formatting text?Each tab contains several groups of related commands. For example, the Font group on the Home tab contains commands for formatting text in your document.
|