I’m using cursors to retrieve data from a database. I know how to retrieve entire columns to use on listviews and such, but I’m struggling to find a way to retrieve a single value.
Let’s say I have a table with two columns (“_id” and “Name”) and I have ten records (rows) in that table. How would I get, for example, the Name in the third row? Considering I defined a cursor that reads that table:
public Cursor getMyNameInfo() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String sqlTables = "MyNames";
qb.setTables(sqlTables);
Cursor c = qb.query(db, null, null, null,
null, null, null);
c.moveToFirst();
return c;
}
Instead of
c.moveToFirst()usec.moveToPosition(2)(Cursorindexes are zero-based hence ‘2’ is the third record).Remember to check that the
Cursorhas valid data first though.EDIT:
Once you’ve moved the cursor to the 3rd record as I explain above, use the following to just get the value of the “Name” column.