Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room inserts duplicate entities

I'm using Android's Room library for the database interaction in an application and I'm sort of stumped on how to prevent duplicate entries from being inserted into the database.

I feel like I must be missing something because this seems like it should be simple to do. I've searched Google for various combinations of words relating to the subject to no avail.

I'm essentially using what one of the samples does for inserting and querying.

The Entity:

@Entity(tableName = "cameras")
public class CameraEntity {
    @PrimaryKey(autoGenerate = true)
    private int id;
    private Integer accountId;
    private Integer dvrId;
    private String vendorId;
    ...
}

The DAO:

@Dao
public interface CameraDao {

    @Query("SELECT * FROM cameras")
    Flowable<List<CameraEntity>> getCameras();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<CameraEntity> values);
}

In terms of the Room library, is there any way to set some rules on when data should be inserted? One post I read mentioned that the auto increment ID is causing each item to be unique in terms of the primary key. If that's true, how are others using this library accounting for that?

Thanks!

like image 527
andsec Avatar asked Oct 24 '17 17:10

andsec


People also ask

Is Android room an ORM?

Is Android Room an ORM? Room isn't an ORM; instead, it is a whole library that allows us to create and manipulate SQLite databases more easily. By using annotations, we can define our databases, tables, and operations.

What is Androidx room?

Room is a Database Object Mapping library that makes it easy to access database on Android applications. Rather than hiding the details of SQLite, Room tries to embrace them by providing convenient APIs to query the database and also verify such queries at compile time.

What is embedded in Room Android?

room. Embedded instead. Can be used as an annotation on a field of an Entity or Pojo to signal that nested fields (i.e. fields of the annotated field's class) can be referenced directly in the SQL queries. If the container is an Entity , these sub fields will be columns in the Entity 's database table.


1 Answers

I stuck this situation on first application with room database. I need something to if that column data exist update other data in row else insert to table

I've one primary key and one uid(here is accountId or dvrId) that are also like primary key and should not be repeated.

For doing this you have to create indices for Entity and put all columns that you don't want replace in it like this

@Entity(tableName = "cameras", indices = [Index(value = ["accountId","dvrId"], unique = true)])
public class CameraEntity {
    @PrimaryKey(autoGenerate = true)
    private int id;
    private Integer accountId;
    private Integer dvrId;
    private String vendorId;
}

And don't forget choose REPLACE strategy

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<CameraEntity> values);

Now if any of id and accountId and dvrId exist in the same column data will update otherwise data will insert at new row

Hope it help

like image 131
Radesh Avatar answered Sep 18 '22 09:09

Radesh