Skip to content
Advertisement

Getting UNIQUE constraint failed: tbl_data.id (Sqlite code 1555 SQLITE_CONSTRAINT_PRIMARYKEY) error in android studio room

I’m getting this error while i have initialize the id as primary key and i added the unique attribute.

Data class code :

    @Entity(tableName = "tbl_data" , indices = {@Index(value = {"id"},
    unique = true)})
    public class Data {
    @PrimaryKey
    private Integer id ;
    private Integer year;
    private Double dowry;

public Data(Integer id,Integer year, Double dowry) {
    this.id = id;
    this.year = year;
    this.dowry = dowry;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getYear() {
    return year;
}
public void setYear(int year) {
    this.year = year;
}
public Double getDowry() {
    return dowry;
}
public void setDowry(Double dowry) {
    this.dowry = dowry;
}

Data Base Class code :

@Database(version = 1 , exportSchema = false , entities = {Data.class} )
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase appDatabase;

public static AppDatabase getAppDatabase(Context context) {
    if (appDatabase == null)
        appDatabase = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class,
                "db_app")
                .allowMainThreadQueries().fallbackToDestructiveMigration()
                .build();
        return appDatabase;
    }

    public abstract DataDao getDataDao();
}

and main activity code :

    db = AppDatabase.getAppDatabase(this);
    dataDao = db.getDataDao();

    dataDao.insertC(new Data(1,1315, 0.004 ), new Data(2,1316, 0.005), new Data(3,1317,  0.006), new Data(4,1318, 0.007));

and full error in logat :

2021-11-07 12:20:45.070 24994-24994/com.mohajer.kitset2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mohajer.kitset2, PID: 24994 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mohajer.kitset2/com.mohajer.kitset2.MainActivity}: android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: tbl_data.id (Sqlite code 1555 SQLITE_CONSTRAINT_PRIMARYKEY), (OS error – 2:No such file or directory) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) at android.os.Handler.dispatchMessage(Handler.java:112) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) Caused by: android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: tbl_data.id (Sqlite code 1555 SQLITE_CONSTRAINT_PRIMARYKEY), (OS error – 2:No such file or directory) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:841) at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:803) at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86) at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:51) at androidx.room.EntityInsertionAdapter.insertAndReturnIdsArrayBox(EntityInsertionAdapter.java:199) at com.mohajer.kitset2.DataDao_Impl.insertC(DataDao_Impl.java:50) at com.mohajer.kitset2.MainActivity.onCreate(MainActivity.java:36) at android.app.Activity.performCreate(Activity.java:7458) at android.app.Activity.performCreate(Activity.java:7448) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)  at android.os.Handler.dispatchMessage(Handler.java:112)  at android.os.Looper.loop(Looper.java:216)  at android.app.ActivityThread.main(ActivityThread.java:7625)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 

Advertisement

Answer

Using @PrimaryKey implies a UNIQUE constraint so there is no need for a unique index on the id column (the index is just a waste).

Hard coding the inserts with id values when inserting (without an onConflict constraint that IGNOREs the conflict) will, after running once from a fresh install (a rerun), result in the the UNIQUE conflict as id’s 1,2,3 and 4 already exist.

Coding the @Dao as @Insert(onConflict = OnConflictStrategy.IGNORE) will ignore the conflict without failing and not insert the duplicates.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement