I know there are lots of similar question to mine but for my situation none of them worked!
I have table with 4 columns and I have set @PrimaryKey(autoGenerate = true) private int id;
for my ID and @index unique
for my other 3 column , Something like this :
@Entity(tableName = "Data" ,
indices = {@Index(value = {"name","data","label"}, unique = true)})
public class Data {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private String data;
private String label;
public Data(String name, String data, String label) {
this.name = name;
this.data = data;
this.label = label;
}
and for my Dao I have set (onConflict = OnConflictStrategy.REPLACE)
for my Insert query , also It should be Sayed that I have test With OnConflictStrategy.IGNORE
and it Didn't even show the Table in App inception :
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Data data);
and finally in my MainActivity
class I have my String Data that is 2d array next to my inserting method :
db = AppDatabase.getAppDatabase(this);
dataDao = db.getDataDao();
ArrayList<Data> dataList = new ArrayList<>();
try {
for (int i = 0; i < stringData.length;) {
data = new Data(stringData[i][0],stringData[i][1],stringData[i][2]);
dataDao.insert(data);
i++;
}
}catch (Exception e){
Log.e(TAG, "onCreate: ", e);
}
I have test and debug my app several times the only thing I got was Error for version number that I forgot to increase! in picture below u can see that my ID got duplicated and it start from 687!
You can see my string data from Here , Although that I have checked it serval time for null or missing part but I couldn't find any.
According to the string data you have linked to in your question, there are only 686 topmost elements, to be inserted.
What would appear to have happened, is that some rows existed that were replaced thus effectively increasing the id but also leaving a gap and hence why your screen shot shows more but there are likely gaps. As an example running the code a second time results rows with id's 687-1372.
This was ascertained using :-
try {
Log.d(TAG,"Trying to insert " + TestData.stringData.length + " rows"); //<<<<<<<<< ADDED
for (int i = 0; i < TestData.stringData.length;) {
....
Furthermore, there is an issue with line 216 it only has 2 parameters not the required 3. To circumvent this I used :-
{" گردش زمین (Earth's velocity)", "0.0000335965","??????????"}, //<<<<<<<<<< only 2 values added ??????????
using you data with the above line fixed and using App Inspection the last rows are :-
I'd suggest uninstalling the app and trying again, after amending the errant line 216 (see below). I'd also suggest using OnConflictStrategy.IGNORE
and also utilising:-
@Insert(onConflict = OnConflictStrategy.IGNORE)
long insertIgnore(Data data);
If the value returned from the insert is -1 then the row would not have been inserted due to the UNIQUE constraints.
New Error I have got from making the table :
2022-03-03 16:17:29.382 27696-27707/com.mohajer.kitset A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x10 in tid 27707 (HeapTaskDaemon), pid 27696 (.mohajer.kitset)