简体   繁体   中英

How to retrieve a string array from a cursor

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM