1

I have a cursor, called passableCursor, containing multiple fields, However one field,ingredients, holds multiple values and I'm unable to get all the values from it.

Example of retrieving from name

Log.d("recipeName", passableCursor.getString(passableCursor.getColumnIndex("name")));

Example of cursor structure

name-> name1

description -> description1

ingredients -> ingredient1, ingredient2, ingredient3.

I've been using getString for the name and description field. However, I'm struggling to get the values from ingredients. There doesn't seem to get a getStringArray method.

2
  • Option #1: Have a separate table for ingredients. Option #2: Convert your string array of ingredients to and from a string (e.g., JSON, CSV), storing that string in the column. Commented May 8, 2017 at 18:06
  • I'm currently doing the former but I was just wondering if there was a method I was missing out on Commented May 8, 2017 at 18:23

1 Answer 1

1

Here is one way:

    SQLiteDatabase db  =null;//Initialize this first
    Cursor cursor = db.rawQuery("your query", null);//Replace with your query(e.g. SELECT FROM table WHERE something=2)
    String preChanged = cursor.getString(cursor.getColumnIndex("row_name"));//Replace row name with your row name
    String[] finalR = preChanged.split(",");//Can be changed to parts
    //String[] finalR = new String[parts.length];
    //for(int i = 0; i < parts.length; i++){
    //    finalR[i] = parts[i];//This for-loop is if you have special needs. If you want to convert the String to a different type(integer, boolean, etc), or you want to make sure it contains/does not contain something.
    //}//Uncomment if needed

    //Now, this needs a special compression method, to ensure correct format;
    //raw is the raw array, continuing from up above we use finalR to make sure you as a reader understand the context

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < finalR.length; i++) {
        if(i != finalR.length - 1)
            builder.append(finalR[i] + ",");
        else
            builder.append(finalR[i]);
    }

This can be converted into a series of methods as well.

Explanation of how this works:

1) Take raw array input(String in this case)

2) Create a single String, separating the different Strings with a ,. For longer Strings where there may be a comma, you may want to consider adding a backslash to the comma, and split at "\,"(two backslashes to make Java register it as an actual backslash, and not an escape char)

3) Save the String to your database

4) Load the String

5) It splits the string at ,, giving you several pieces. This is the array, and should be(assuming you have taken precautions protecting the system from in-string commas(,)

6) OPTIONAL: Refine the String array, by removing something, adding something, adding support for markup(replace [link text](http://example.com) with HTML code), change the type(string -> integer), whatever you need.

NOTE

This is the basic draft. This allows you to save basic arrays to a database, and recover it without having to create multiple rows. It may need refining, if you save in the String array text with commas, or any char that may attempt to cause SQL injection.

This can be refined to work with any other types of arrays, for an instance long, integer or boolean, but the saving is the same(it has to be compressed to a string), but you add .parse[Type](input) to convert it to an array of the desired type

Sign up to request clarification or add additional context in comments.

2 Comments

getString only returns the first element in the array, so split isn't a solution
getString on a Cursor gets the String from a column in the SQL table. Using the compression(last code snippet) it compresses the entire array into a single String that goes in a single row. That is why this requires both components. But in return, you only need one row for each array. If a row contains this is one string, this is another, and a third array element, it splits it at the comma in the code, but not in SQL, pushing the three different pieces into the same array. That array can then be refined, displayed, whatever you need to do with it.

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.