Cursor cursor = db.rawQuery("SELECT age FROM students WHERE id=123", null); if (cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndex("age"); long age = cursor.getLong(columnIndex); // use age value } cursor.close();
Cursor cursor = db.rawQuery("SELECT sum(salary) FROM employees", null); if (cursor.moveToFirst()) { long totalSalary = cursor.getLong(0); // use totalSalary value } cursor.close();In this example, we execute a SELECT query on the "employees" table with the SUM function to calculate the total salary of all employees. We then move the cursor to the first record and get the total salary using the getLong method with the index parameter 0. The package library for the Cursor class is android.database.