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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:28:00+00:00 2026-05-30T09:28:00+00:00

Currently I have a query that looks at the table and returns the newest

  • 0

Currently I have a query that looks at the table and returns the newest row, reads that row and sets the string to the value in the chosen column index.

At the moment I have 9 columns so I have end up making 9 methods for returning each column just to return a string and set it to each individual textview.

Do I make a cursor to return the row and then set the column index when I am setting the textviews. Something like

            cursor.getString(1)

This is my current query

            public String getName() {
    // TODO Auto-generated method stub
    String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_COLUMN2,
            KEY_COLUMN3, KEY_COLUMN4, KEY_COLUMN5, KEY_COLUMN6, KEY_COLUMN7, KEY_COLUMN8 };
    Cursor c = mDb.query(true, DATABASE_TABLE, columns, null, null, null,
            null, "_id DESC", "1");

    if (c != null) {
        c.moveToFirst();
        String name = c.getString(1);
        return name;
    }

    return null;
}
  • 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-30T09:28:02+00:00Added an answer on May 30, 2026 at 9:28 am

    You could easily change getName() to something field-agnostic, such as:

    public String getField(String fieldName) {
        ...
        String s = cursor.getString(cursor.getColumnIndexOrThrow(fieldName));
    }
    

    And use it like:

    String s = mDb.getField(mDb.KEY_NAME);
    

    A problem with this approach is that you can’t use getString() for a numerical column, so you wind up with multiple methods for each datatype supported in your table. A way to tackle this is to look into the cursor’s getType() method or just return a complete data structure from your database access methods. Say you have:

    DB Column        Field Type
    FOO              varchar
    BAR              integer
    BAZ              varchar
    

    If you define a class, say, MyRowMapper you can do something like this:

    public MyRowMapper getRow(String... queryParameters) {
        //query table based on queryParameters
        MyRowMapper mapper = null;
        if (cursor.moveToFirst()) {
            String foo = cursor.getString(cursor.getColumnIndexOrThrow("FOO"));
            Integer bar = cursor.getInteger(cursor.getColumnIndexOrThrow("BAR"));
            String baz = cursor.getString(cursor.getColumnIndexOrThrow("BAZ"));
    
            mapper = new MyRowMapper(foo, bar, baz);            
        }
        cursor.close(); // NEVER FORGET TO CLOSE YOUR CURSOR!
        return mapper;
     }
    

    This is just abstract mailercode, so take the syntaxt with a grain of salt. But hopefully you get the idea.

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

Sidebar

Related Questions

I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I currently have a SQL query that returns a number of fields. I need
Hey guys I have a query that currently finds the latest comment for each
Dear all, I have a select query that currently produces the following results: DoctorName
I have a table that looks like this: episodes ------------------------------------------------------------ id (PK serial) |
I have a view that basically just returns all records from a table, and
I have the following schema, and would like to do a query that returns
I have a table in MySQL that looks like this: studentID subjectID anotherID anotherID2
I have a database query that returns ItemID, ItemName, CategoryID and CategoryName. I am
I have a table code_prices that looks something like this: CODE | DATE |

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.