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.

  • 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. – CommonsWare May 08 '17 at 18:06
  • I'm currently doing the former but I was just wondering if there was a method I was missing out on – Mcseth Antwi May 08 '17 at 18:23

1 Answers1

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • getString only returns the first element in the array, so split isn't a solution – Mcseth Antwi May 08 '17 at 19:17
  • 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. – Zoe May 08 '17 at 19:24