Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:37:47+00:00 2026-05-26T23:37:47+00:00

It’s a pretty straightforward question. I’m hoping the answer is both, but I’m worried

  • 0

It’s a pretty straightforward question. I’m hoping the answer is “both”, but I’m worried it’s neither. I’ve scrutinized the Android developer docs for SQLiteDatabase and Cursor, but can’t find any definitive answer to this question.

The case I’m asking about is where I get a cursor, call moveToFirst, then loop until isAfterLast returns true. It would be really convenient for a pattern I’m coding a few times if that would just work and execute the loop 0 times if the cursor has 0 records.

Or do I need to explicitly test for an empty cursor first?

EDIT: Some of the responses indicate that people aren’t quite getting what I’m asking. Basically, I would like to write this:

cursor = myDb.query(...)
cursor.moveToFirst()
while (!cursor.isAfterLast()) {
    processRow(...)
}

But I’m not 100% certain that I can. isAfterLast can’t return anything useful but true for this case, but that doesn’t mean it actually does return true for an empty query. The docs don’t seem to specify what the return value of most of the cursor methods are if the cursor is empty (the Android docs seem to be unspecific on a lot of corner cases, actually), other than getCount. So I’m worried that I need to do this:

cursor = myDb.query(...)
if (cusror.getCount() > 0) {
    cursor.moveToFirst()
    while (!cursor.isAfterLast()) {
        processRow(...)
    }
}

Which is messier and logically redundant. Bear in mind that in the real implementation there’s more code, and this is spread across multiple methods. And one of the answers is now suggesting that I need to check the cursor for null as well, which is completely undocumented…

I simply want to know whether anyone else knows the actual behaviour of isAfterLast when called on an empty cursor.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:37:47+00:00Added an answer on May 26, 2026 at 11:37 pm

    I don’t use isAfterLast. I use the following pattern to read the query result.

        SQLiteDatabase db = new DbHelper().getWritableDatabase();
        String sql = "SELECT ...";
        String[] args = ...;
        Cursor cs = db.rawQuery(sql, args);
        while (cs.moveToNext()) {
            // cs.getXxx(0), cs.getXxx(1)
        }
        cs.close();
        db.close();
    

    DbHelper is a class that I extend from SQLiteOpenHelper

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.