Application example of multi table query in Android room database
Android room database (Introduction)
preface
In SQLite database, we can specify the relationship between objects, so we can bind one or more objects to one or more other objects. This is the so-called one to many and many to many relationship.
Since you want to query multiple tables, you must have associations between tables. At this time, we have to use the new annotation @ ForeignKey
For the next content, we need the content of the previous section
Let's create a new table associated with it
Here, I use @ ForeignKey to associate the company table, primary key ID and foreign key EMP_ ID, followed by the unique index created using indexes.
The following is how to create Dao
The last step is to create a database
Here I think everyone is familiar with the previous article introduction. There is not much explanation here. If you don't remember, please see the previous article.
Specific use
In this way, we will insert the required data into the database. Let's query to see if the insertion is successful.
The query code is as follows:
So it seems that the insertion was successful...
Well, let's start the table query.
When we query, we will encounter a problem, that is, we associate the two tables, and the queried fields are after the combination of company and departemnt. So here we need to create a model class again.
Because we have written the query method above, we can call it directly.
The query results are as follows, indicating that our multi table query is successful:
Here, the multi table query is over. It will be updated later.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.