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 8512283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:14:13+00:00 2026-06-11T04:14:13+00:00

I am trying, unsucessfully, to query my database to find the maximum ‘area number’,

  • 0

I am trying, unsucessfully, to query my database to find the maximum ‘area number’, in my areas table for a certain inspection, so that I can set the text in a form to the next area number.

The database table consists of four columns; _id, inpsection_link, area_number, area-reference.

I have created the following in my database helper class (using this post as a guide: SQLiteDatabase.query method):

 public int selectMaxAreaNumber (long inspectionId) {
    String inspectionIdString = String.valueOf(inspectionId);
    String[] tableColumns = new String[] { 
            AREA_NUMBER, 
            "(SELECT max(" + AREA_NUMBER + ") FROM " + AREAS_TABLE + ") AS max" 
        }; 
    String whereClause = INSPECTION_LINK + " = ?";
    String[] whereArgs = new String[] {
            inspectionIdString          
        };
    Cursor c = rmDb.query(AREAS_TABLE, tableColumns, whereClause, whereArgs, 
            null, null, null);
    int maxAreaNumber = c.getColumnIndex("max");
    return maxAreaNumber;
}

Which I then call in the areaEdit class as follows:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    rmDbHelper = new RMDbAdapter(this);
    rmDbHelper.open();
    Intent i = getIntent();
    inspectionId = i.getLongExtra("Intent_InspectionID", -1);
    areaId = i.getLongExtra("Intent_AreaID", -1);
    if (areaId == -1) {
        nextAreaNumber = rmDbHelper.selectMaxAreaNumber(inspectionId) + 1;
        Toast.makeText(getApplicationContext(), String.valueOf(nextAreaNumber), 
                Toast.LENGTH_LONG).show();  
    }
    setContentView(R.layout.edit_area);
    setUpViews();
    populateFields();
    setTextChangedListeners();
}

However, it just returns 1 everytime (even if there are numbers higher than that stored in the database).

Confused.com!! Any help much appreciated.

  • 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-06-11T04:14:15+00:00Added an answer on June 11, 2026 at 4:14 am

    Your issue is here :

    int maxAreaNumber = c.getColumnIndex("max");
    

    You’re getting the column index of max, which is 1 because you only have one column in your query.
    Instead, do something like this :

    int maxAreaNumber = 0;
    if(c.moveToFirst())
    {
        maxAreaNumber = c.getInt(1);
        // or cleaner
        maxAreaNumber = c.getInt(c.getColumnIndex("max"));
    }
    else
        // no data in cursor
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Possible Duplicate: Java Class.cast() vs. cast operator I am unsuccessfully trying to find out
Trying to get two different counts in one query. To do this I am
I'm trying to set up page anchors on a website that uses mod_rewrite (Apache2
I have a query that is working fine when I run in through phpMyAdmin
I have a WCF service that throws an exception which I am trying to
I have been trying unsuccessfully to mirror text that only matches a-z0-9 using the
I have been trying unsuccessfully to find a way to filter out records from
I have been trying to use a C# Regex unsuccessfully to remove certain strings
Newbie alert... I'm trying to make an educational that uses different audio players (with

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.