Answer the question
In order to leave comments, you need to log in
android: how to correctly insert arrays into sqlite database?
I'm trying to add values from arrays to the corresponding columns (first array to first column, second array to second, third to third) using loops. Everything works, only the second array starts to be added not to the first line of the corresponding column, but to the line following the one in which the last value of the first array was written. In the first column on the next lines, the last value begins to be duplicated. The same happens with the third and second columns. I think it will be clearer:
Column1 Column2 Column3 Value1 Value2 Value3 Value3 Value1 Value3 Value2
Value3
Value3
Value3
Value3
Value1
Value3
Value3
Value2
Value3 Value3 Value3
Please suggest the correct solution to the problem.
The code:
ContentValues cv = new ContentValues();
db = dbHelper.getWritableDatabase();
for (int i = 0; i < array1.length; i++) {
cv.put("c1", array1[i]);
db.insert("mytable", null, cv);
}
for (int i = 0; i < array2.length; i++) {
cv.put("c2", array2[i]);
db.insert("mytable", null, cv);
}
for (int i = 0; i < array3.length; i++) {
cv.put("c3", array3[i]);
db.insert("mytable", null, cv);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question