This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

background

It is inevitable to use database in a project, and there are various tripartite frameworks. In this case, I chose Room from Google Jetpack component. The previous article briefly introduced how to create database and data table in Room, and this article simply introduced the basic operation of data table

Create Dao

In Room, the Dao is an interface, decorated with @DAO annotations

@Dao
public interface TestDao {}Copy the code

Add data

  • through@InsertAnnotation specifies the method as the add data method, the default processing mode is to ignore the current data

Since it can be a single piece of data or a set of data when added, the mutable array is used here

@Insert
void add(TestEntity... entities);
Copy the code
  • Change the add data mode

The @insert annotation ignores the current data by default. What if there is a need to replace it

By specifying the @ Insert annotations to solve OnConflictStrategy onConflict. REPLACE: if there is the old data will be replaced, if there is no Insert OnConflictStrategy. ROLLBACK: if there is the old data will be rolled back, if not just insert OnConflictStrategy ABORT: if the presence of old data may be terminated, if there is no insert OnConflictStrategy. FAIL: if there are old data exist will be prompted to insert the data failure, if not insert OnConflictStrategy. IGNORE: if there are the old data is to IGNORE the current data, if there is no insert Use the following

@Insert(onConflict = OnConflictStrategy.REPLACE)
void add(TestEntity... entities);
Copy the code

Delete the data

Specify the method as the Delete data method with the @delete annotation

@Delete
void delete(TestEntity... entities);
Copy the code

Update the data

Specify the method as the Update data method through the @update annotation

@Update
void update(TestEntity... entities);
Copy the code

Call logic

  • inAppDatabaseDefines Dao corresponding methods in
public abstract TestDao testDao(a);
Copy the code
  • Define the implementation class corresponding to the Dao interface as a singleton, and obtainAppDatabaseobject
public class TestRepo {
    private static TestRepo sInstance;

    private final AppDatabase mDatabase;

    private TestRepo(final AppDatabase database) {
        mDatabase = database;
    }

    public static TestRepo getInstance(a) {
        if (sInstance == null) {
            synchronized (TestRepo.class) {
                if (sInstance == null) {
                    AppDatabase database = AppDatabase.getInstance();
                    sInstance = newTestRepo(database); }}}returnsInstance; }}Copy the code
  • Encapsulate the method and call the corresponding add, delete, and modify methods in the Dao

Since add defines a mutable array in the Dao, two methods are created in the Dao implementation class, one for a single piece of data and one for multiple pieces of data, to be called

The following is only an example of the add method. The deletion and modification are similar

public void addResults(TestEntity entity) {
    mDatabase.testDao().add(entity);
}

public void addResults(List<TestEntity> entities) {
    int size = entities.size();
    TestEntity[] amHeartRateEntities = new TestEntity[size];
    for (int i = 0; i < size; i++) {
        amHeartRateEntities[i] = entities.get(i);
    }
    mDatabase.testDao().add(amHeartRateEntities);
}
Copy the code