Moster 06 manage sq lite databases with room#6
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Moster 06 manage sq lite databases with room
1. Define a database table with Room
We using app.cacheDir for get the cache's address from the device.
We add anotation
2. Define SQL operations in a DAO interface
We create the Dao class. The purpose of a DAO interface is to define database operations, queries, insert, update and delete operations and so on.
3. Define an SQLite database with Room
For each entity class, start with the name of the class, then colon, colon class.(If I had more than one entity I'd list them all here)
The version is always a whole number and it always starts with one for the first version of your database, but then, if you change your database structure later you'll upgrade your version, finally I'll add an export schema property and I'll set that to false. And that just means don't generate files to document the database.
Then I'll assign instance using this expression: room.databasebuilder.
4. Insert and retrieve data with Room
I'll create an instance of my data access object. I'll call it monsterDao and I'll initialize it with this expression. MonsterDatabase.getDatabase. I'll call MonsterDao. And now I have an object I can use in memory to work with that database table.
I'll create a co-routine so that I'm doing all this work in a background thread.
In a background thread and the toast architecture has to be called from a foreground thread. But co-routines let you switch threads on the fly.
I'll call postValue and I'll pass in the data value.