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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:02:50+00:00 2026-06-09T12:02:50+00:00

I’m writing method wrapping db SELECT statement, that receives SQL query and runs it.

  • 0

I’m writing method wrapping db SELECT statement, that receives SQL query and runs it. Result is collected in Object[][]. Every cell in database may be Integer or String.

I have problem with return array. It’s Object[][], so I loose type.

public Object[][] select(String query) {
    Object[][] result = new Object[cursor.getCount()][cursor.getColumnCount()];
    Cursor cursor = db.rawQuery(query, null);

    //Iterate over cursor (rows)
    do {
        //Iterate over columns (cells with data of different type)
        for(int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
            int type = cursor.getType(columnIndex);

            //assume, that there are only two types - String or Integer (actualy there are 3 more types)
            if(type == Cursor.FIELD_TYPE_INTEGER) {
                result[cursor.getPosition()][columnIndex] = cursor.getInt(columnIndex);
            }
            else if(type == Cursor.FIELD_TYPE_STRING) {
                result[cursor.getPosition()][columnIndex] = cursor.getString(columnIndex);
            }
        }
    } while (cursor.moveToNext());

    return result;
}

I do not want to loose type. E.g. want to be able do like this (pseudocode):

//pseudocode:
result = select("SELECT age, name FROM people")
print( "Hello, " + result[0][0] + ". In ten years you'll be " )
print( result[0][0] + 10 )
//Prints: Hello, John. In ten years you'll be 56

Every book about Java says, that I can’t have array of different types. It’s clear.

But how should I hold all this Integers and Strings from DB? I tried to use Collections and Generics, but no luck – new to Java.

Also tried to use some kind of Result value holder:

private class Result {
    private Class type;
    private Object value;

        ...

    public ??? get() { //??? - int or string - here I have some problems :)
        return this.type.cast(this.value);
    }

    //do NOT want to have these:
    public int getInt() { return (int) value; }
    public int getString() { return (String) value; }
}

Method select(...) returns Result[][] in this case.

Answered myself:
Understood, that I was trying to code in Java like I did in php. With strong typing I can not make straightforward method, returning all types without explicit casting.

After all I decided to create common data objects and fill them in on selects.

  • 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-09T12:02:51+00:00Added an answer on June 9, 2026 at 12:02 pm

    Return a List of Maps with the columns names or indexes as keys like this:

    public List<Map<Integer,Result>> select(String query)
    

    When extracting your data do this:

    List<Map<Integer,Result>> data = new ArrayList<Map<Integer,Result>>();
    ...
    Map<String,Result> elem = new...
    elem.put(columnIndex, new Result(..));
    
    data.add(elem);
    

    Collections are surely more powerful than Arrays.

    EDIT
    Used the Result holder class you provided.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.