There is a certain table in SQLite from it we display all values ​​in a string variable.
Here is what I realized:

Cursor cursor = mDB.rawQuery("SELECT * FROM "+ TABLE, null); int result = cursor.getColumnCount(); int result1 = cursor.getCount(); int result2 = result*result1; // количество значений в таблице (получилось то, что нужно) String [] def = new String[result2-2]; def [0] = " 1 "; //первое значение мне нужно своё и дальше я добавляю данные начиная с 3. for (int d = 0; d < result1-1; d++) { Cursor cursor1 = mDB.rawQuery("SELECT * FROM "+ TABLE + " WHERE ID = " + d, null); if (cursor1.moveToFirst()) { for (int j = 0; j < result2; j++){ do { for (int k = 3; k < result-1; k++){ j=+1; def [j] = cursor1.getString(k); /* здесь ошибка E/AndroidRuntime: FATAL EXCEPTION: main Process: anem.tr, PID: 2580 android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1 */ } } while (cursor1.moveToNext());} } } return def; } 

From an error, I can understand that my cursor is not on that value and just does not move further, but where I can’t figure it out, so I ask for help. In the simple case, when I work with one line, everything works.

  • remove in all conditions the equal sign d < = result1-1 - JVic
  • Thank you, the sign is removed, the error remains the same. - Antonem

3 answers 3

 cursor.moveToFirst(); while (!cursor.isAfterLast()) { //Здесь берёшь данные из курсора и делаешь с ними всё что нужно cursor.moveToNext(); } cursor.close(); 

And review the logic in the cycles, I'm sure that there you can write everything much more elegantly.

  • Yes, I'm old school, I would like to say, but I feel a lack of knowledge ... - Antonem
  • So to say, when rearranging the terms, the amount does not change. The error is the same. - Antonem

j = + 1; This assignment value = 1, after the first iteration, j-2 will be = -1, hence the error. Perhaps instead of j = + 1, j + = 1 was meant.

  • Hike it is, but now - Antonem
  • E / AndroidRuntime: FATAL EXCEPTION: main Process: PID: 3040 android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1 - Antonem
  • I told you that the algorithm needs to be revised, but in the array, the numbering starts from 0, for (int j = 3; j <= result2-1; j ++) - here j is initialized 3, def [j-2] - here we go by index [1], if there is only 1 element in the array, then its index is 0. - AZ
  • First, I put my value in an array with index 0, then I put a value from the third column of the table in an array with index 1, increasing the value of j simultaneously, so that each new value is written with a new index and until the line ends. It only occurred to me that it makes no sense to start with 3, you should start with 1, then there is less confusion, but the error is certainly not the case. - Antonem
  • There was a small error in the name of the array. - Antonem

He sat down himself, considered, in the end got this answer:

  int result2 = (cursor.getColumnCount()*cursor.getCount())-(3*(result1)); String [] def = new String[result2+1]; int j = 0; do { Cursor cursor1 = null; do { for (int d = 0; d < result1; d++) { cursor1 = mDB.rawQuery("SELECT * FROM "+ TABLE + " WHERE ID = " + d, null); if (cursor1.moveToFirst()) { for (int k = 3; k < result; k++){ j++; def [j] = cursor1.getString(k); } } } } while (cursor1.moveToNext()); } while (j == result2+1); return def; 

Displayed only the changed code, it turned out almost all.
The first error was in determining the number of values ​​in the table, as it turned out during normal multiplication, values ​​were lost, so I had to rethink and rewrite the formula, where I remove the first three columns from the table and write the rest to the array.
The second error in the arrangement of for loops, first I selected a string, then I went through the whole array in an attempt to write the first string into the entire array, which was clearly larger than the given string. As a result, I changed two cycles in places and it turned out, as I need, at first the array starts with 0 value, which I have already assigned. Then we take the string, write the third value of the string into the array with index 1 and in parallel increase the value of j, the line ends, take a new one and so until the value of j is equal to the entire array (the array is larger by 1 value, because first we write to array with index 0 "1").
I do not know how to increase karma to all who answered, because their responses also indicated my mistakes.