import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class MyDatabaseHelper { private SQLiteDatabase database; private Cursor cursor; public void query(String tableName) { cursor = database.rawQuery("SELECT * FROM " + tableName, null); if (cursor.moveToLast()) { // retrieve values from cursor } else { // cursor is empty } } }
import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; public class ContactReader { private ContentResolver contentResolver; public void readLastContact() { Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = {ContactsContract.Contacts._ID}; cursor = contentResolver.query(uri, projection, null, null, null); if (cursor.moveToLast()) { // retrieve last contact ID } else { // no contacts found } } }In this example, moveToLast is used to move the cursor to the last row in the result set of a query to the ContactsContract, a package in the Android library for accessing contact information. Package library: android.database.