Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room persistence SQLiteConstraintException when inserting items twice

I have been working with the Room persistence library which worked great until I items in two different insertions.

Sound Entity class

@Entity
public class Sound {
@PrimaryKey(autoGenerate = true)
private int sid;
@ColumnInfo(name = "sound_theme")
private int soundTheme;
@ColumnInfo(name = "sound_name")
private String soundName;
@ColumnInfo(name = "sound_resource")
private int soundResource;
@ColumnInfo(name = "sound_image_resource")
private int soundImageResource;
@ColumnInfo(name = "sound_favorite")
private boolean isFavorite;
@ColumnInfo
private int usedCount;
@ColumnInfo
private long lastUsed;


 public Sound(int soundTheme, String soundName, int soundResource, int soundImageResource, boolean isFavorite, int usedCount, long lastUsed) {
    this.soundTheme = soundTheme;
    this.soundName = soundName;
    this.soundResource = soundResource;
    this.soundImageResource = soundImageResource;
    this.isFavorite = isFavorite;
    this.usedCount = usedCount;
    this.lastUsed = lastUsed;
}

Watch that I have a sound id as a primary key which auto-generates.

Sound DAO

@Dao
public interface SoundDao {

@Query("SELECT * FROM sound")
List<Sound> getAll();

@Query("SELECT * FROM sound WHERE sid IN (:soundIds)")
List<Sound> loadAllByIds(int[] soundIds);

@Query("SELECT * FROM sound WHERE sound_name LIKE :name LIMIT 1")
Sound findByName(String name);

@Query("SELECT * FROM sound WHERE sid LIKE :id LIMIT 1")
Sound findById(int id);

@Insert
void insertAll(Sound... sounds);

@Insert
void insertAllArrList(ArrayList<Sound> sounds);

@Delete
void delete(Sound sound);

@Query("DELETE FROM sound")
public void deleteAllSounds();
}

The error goes with this line:

      if (focusedSound.isFavorite()) 
      favorites.soundDao().insertAll(focusedSound); 
      else favorites.soundDao().delete(selectedSound);

first insertion goes well(If you insert multiple items in this it will also work), second time you insert it crashes. I assume it has something to do with the primary key for the sound id(sid) but I am not sure how to solve it.

here are some of the error logs:

06-03 20:03:36.454 8807-8807/com.zaid.green.soundpress E/InputEventReceiver: 
Exception dispatching input event.
06-03 20:03:36.454 8807-8807/com.zaid.green.soundpress E/MessageQueue-JNI: 
Exception in MessageQueue callback: handleReceiveCallback
06-03 20:03:36.460 8807-8807/com.zaid.green.soundpress E/MessageQueue-JNI: 
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: 
Sound.sid (code 1555)
SQLiteConnection at nativeExecuteForLastInsertedRowId(Native Method)  
SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
    at 
like image 878
Gal Avatar asked Jan 27 '26 20:01

Gal


1 Answers

Yes, you are right, the issue is with the primary constraint and duplicate items being insertion.

Solution : define OnConflictStratergy on your @intert methods as

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(Sound... sounds);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllArrList(ArrayList<Sound> sounds);

As you can see, this will replace the new objects with old ones

like image 132
Pavneet_Singh Avatar answered Jan 30 '26 10:01

Pavneet_Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!