2

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;
    }

4 Answers 4

7

Instead of c.moveToFirst() use c.moveToPosition(2) (Cursor indexes are zero-based hence '2' is the third record).

Remember to check that the Cursor has 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.

String theName = c.getString(getColumnIndex("Name"));
Sign up to request clarification or add additional context in comments.

3 Comments

And that will return that single row?
A Cursor only ever points at a single row at any given time hence the use of the moveTo... methods.
@tyb: Also, see my edit for accessing on a single column value.
1
Cursor cursor = dbHelper.getdata();

        System.out.println("colo" + cursor.getColumnCount() + ""
                + cursor.getCount());

        cursor.moveToFirst();

        if (cursor != null) {

            while (cursor.isAfterLast() == false) {

                String chktitle = title.trim().toString();
                String str = cursor.getString(cursor.getColumnIndex("title"));

                System.out.println("title :: "
                        + cursor.getString(cursor.getColumnIndex("title")));
                System.out.println("date :: "
                        + cursor.getString(cursor.getColumnIndex("date")));
                System.out.println("desc :: "
                        + cursor.getString(cursor.getColumnIndex("desc")));

                if (chktitle.equals(str) == true) {
                    tvAddfavorite.setText("Remove Favorite");
                    break;
                }
                cursor.moveToNext();
            }
        }
        cursor.close();

Comments

1

Add a WHERE clause:

qb.appendWhere("_id = 2");

1 Comment

@tyb Before you make the query on the SQLiteQueryBuilder qb.
0

Thanks to the answers above I created the following method to give the value of an item from another column but in the same row.

public String getValueFromColumn(int position, String tableName, String columnToGetValueFrom) {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    qb.setTables(tableName);
    Cursor c = qb.query(db, null, null, null,
            null, null, null);

    c.moveToPosition(position);
    String neededValue = c.getString(c.getColumnIndex(columnToGetValueFrom));
    return neededValue;
}

Hope it helps anyone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.