0

I'm trying to understand how cursors work and I don't understand a portion of this code -- (lifted off of http://www.vogella.com/articles/AndroidSQLite/article.html)

public Comment createComment(String comment) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
        long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
                values);
        Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        Comment newComment = cursorToComment(cursor);
        cursor.close();
        return newComment;
    }

Can someone please explain what is going on here? This is what I THINK is going on - He is inserting values into the table under the column named comment. He then does a query setting the cursor to where he added the comment in the table.

Then I am confused on why he does cursor.moveToFirst(). Isn't the cursor pointing to the current comment he just added? I thought he is trying to return the comment he just inserted into the table, so couldn't he just remove the moveToFirst() method?

4
  • once you 'open' the cursor, you then move to the first (or other) row, then you can move next etc. before closing the cursor. Commented Jun 22, 2012 at 18:55
  • wouldn't that be cursor.moveToNext() until isAfterLast()? but I don't see any evidence of him iterating thru the table Commented Jun 22, 2012 at 18:58
  • Cursor objects do not have a default position when you first acquire them... you need to explicitly move it to a certain position (often the first row) before you can manipulate the data. Commented Jun 22, 2012 at 19:02
  • He's not iterating through the table. He saves the id of the comment he entered, and then queries for that same id. Commented Jun 22, 2012 at 19:02

2 Answers 2

1
  Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);

Returns results are cursor object.

 cursor.moveToFirst();

Make sure your are pointing to first element in the cursor object (or) cursor is not empty.

 Comment newComment = cursorToComment(cursor);

Calling another method to go through the cursor and perform what ever logic coded inside the method, which return Comment object.

 cursor.close();

Close the cursor, so that it would be eligible GC and memeory will be free.

 return newComment;

Return the comment object to caller.

Sign up to request clarification or add additional context in comments.

3 Comments

moveToFirst is also used to check if the cursor is empty (i.e. if it returns false)
i'm starting to understand it, but i just need a little clarification. A cursor points to a row where you tell it correct? Does that mean moveToFirst() points to the first column in the row of where the cursor is pointing?
first row in list of rows. Let us your query returned 10 rows, moveToFirst, puts your cursor at first row, from each row you will get column by doing get(..) operation.
0

From the docs at http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

query returns: "A Cursor object, which is positioned before the first entry."

Then, http://developer.android.com/reference/android/database/Cursor.html

says moveToFirst() : "Move the cursor to the first row."

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.