Cursor cursor = database.rawQuery("SELECT * FROM my_table", null); int columnCount = cursor.getColumnCount(); // Iterate through columns for (int i = 0; i < columnCount; i++) { String columnName = cursor.getColumnName(i); Log.d("ColumnName", columnName); }
Cursor cursor = getContentResolver() .query(CallLog.Calls.CONTENT_URI, null, null, null, null); int numberColumnIndex = cursor.getColumnIndex(CallLog.Calls.NUMBER); int nameColumnIndex = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME); while (cursor.moveToNext()) { String number = cursor.getString(numberColumnIndex); String name = cursor.getString(nameColumnIndex); Log.d("Call", "Name: " + name + ", Number: " + number); }In this example, we are querying the call log table to fetch the name and number of each call. We get the column index of "number" and "name" using getColumnIndex() method of Cursor class. Then we loop over each row using moveToNext() method and get the string value of columns using getString() method of Cursor class. The android.database package is part of the Android SDK and comes with every Android development environment setup.