// Define a query to retrieve a single row from the "users" table String[] projection = {"_id", "name", "email"}; String selection = "_id=?"; String[] selectionArgs = {Integer.toString(userId)}; Cursor cursor = db.query("users", projection, selection, selectionArgs, null, null, null); // Move the cursor to the first row if (cursor.moveToFirst()) { // Retrieve data from the cursor int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String email = cursor.getString(cursor.getColumnIndex("email")); // Do something with the data } // Close the cursor when finished cursor.close();
// Define a query to retrieve all rows from the "users" table String[] projection = {"_id", "name", "email"}; Cursor cursor = db.query("users", projection, null, null, null, null, null); // Move the cursor to the first row if (cursor.moveToFirst()) { do { // Retrieve data from the cursor int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String email = cursor.getString(cursor.getColumnIndex("email")); // Do something with the data } while (cursor.moveToNext()); } // Close the cursor when finished cursor.close();Both examples use the android.database.Cursor class and are part of the android.database package in the Android SDK.