4

I've got a query, (I'm using rawQuery())

  SELECT * FROM <table>

I'm then storing what it returns using a cursor. From their what I want to do is, start at the first row so.. cursor.moveToFirst() then take each column , column by column and store its particular value in a variable. I then want to move onto the next row and do the same. So I guess my question is How would I get cursor to deal with multiple columns?

Thanks,

1 Answer 1

16

I might be missing something here, wouldn't you have a nested loop.

The outer loop cycles through each records:

while (cursor.moveToNext()) {
  ...
  // inner loop here
  ...
}

and the inner loop would cycle through each column

for (i=0; i<cursor.getColumnCount(); i++) {
  ...
  String var1 = cursor.getString(i);
  ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

I know this is already accepted, but a do {}while (cursor.moveToNext()); works better, because if you check .moveToNext first, you skip the first record... Learned from experience...
Are you sure? A cursor typically is sitting at position -1 when returned and calling moveToNext() will move it to the first row. Also, with your logic the code block inside do{} will always execute at least once, even if the Cursor contains no records.

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.