Android development gets database data error "Value must be ≥ 0 but `getColumnIndex` can be -1"

       In Android development, when obtaining the data in the built-in database (SQLite), you will use Cursor.getColumnIndex() to obtain each column of data in a loop, but if you write it directly, you will get an error. We will open Build to view the error and ask you to modify the following The error code.

       Value must be ≥ 0 but `getColumnIndex` can be -1" translate "value must be greater than or equal to 0, but getColumnIndex can be -1".

       Click on this function to view the source code. It can be seen from here that its return value is -1, so it will naturally report an error. Then I saw that it is very similar to him, that is, getColumnIndexOrThrow() . According to the previous prompt, it is concluded that it starts from 0.

 Then use this method to try, the final result is that no error message will be generated and the data can be read.

      In addition, you can also use' @SuppressLint("Range") ', the meaning of @SuppressLint("Range") is: ignore the warning.

Guess you like

Origin blog.csdn.net/qq_62079241/article/details/127050223