Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: SQLite saving string array?

I need to save a string array to the database but it won't let me. This is what I have:

 public long createEntry(String startTime, String endTime, String[] states) {
         ContentValues initialValues = new ContentValues();
         initialValues.put(START_KEY_TIME , startTime);
         initialValues.put(END_KEY_TIME , endTime);
         initialValues.put(KEY_STATE, states );

         return databaseConnect.insert(DATABASE_TABLE, null, initialValues);
}

But if I put string[] states in, it says that content values is not able to take an argument. How do I get around that? I was thinking I have 7 things in states, could I like have 7 separate strings and store stuff in each and then afterwards put all the strings back into an string array? Or would that be bad practice?

like image 662
user1175899 Avatar asked Jan 29 '12 13:01

user1175899


4 Answers

You cannot save String array into Database. But you can use this trick.

1) So you have to convert it into simple String using convertArrayToString(String[] array) method. This will concatenate all elements of string using 'comma'.

2) When you would retrieve this String back from Database you could convert it back to String array using convertStringToArray(String str) method. This method will split the string from 'comma' and you will get your original array back.

public static String strSeparator = "__,__";
public static String convertArrayToString(String[] array){
    String str = "";
    for (int i = 0;i<array.length; i++) {
        str = str+array[i];
        // Do not append comma at the end of last element
        if(i<array.length-1){
            str = str+strSeparator;
        }
    }
    return str;
}
public static String[] convertStringToArray(String str){
    String[] arr = str.split(strSeparator);
    return arr;
}
like image 147
Muhammad Nabeel Arif Avatar answered Sep 22 '22 03:09

Muhammad Nabeel Arif


Based on Muhammed's answer but handles List of Strings instead of array and uses StringBuilder instead of allocating many Strings during values concatenation:

private static final String LIST_SEPARATOR = "__,__";

public static String convertListToString(List<String> stringList) {
    StringBuilder stringBuilder = new StringBuffer();
    for (String str : stringList) {
        stringBuilder.append(str).append(LIST_SEPARATOR);
    }

    // Remove last separator
    stringBuilder.setLength(stringBuilder.length() - LIST_SEPARATOR.length());

    return stringBuilder.toString();
}

public static List<String> convertStringToList(String str) {
    return Arrays.asList(str.split(LIST_SEPARATOR));
}
like image 39
Nati Dykstein Avatar answered Sep 22 '22 03:09

Nati Dykstein


Convert it to single String to save it in sqlite. Later, retrieve it back from sqlite as a single string and convert it back to Array of String.

String ARRAY_DIVIDER = "#a1r2ra5yd2iv1i9der";

public String serialize(String content[]){      
    return TextUtils.join(ARRAY_DIVIDER, content);
}

public String[] derialize(String content){
    return content.split(ARRAY_DIVIDER);
}
like image 9
Aung Thiha Avatar answered Sep 20 '22 03:09

Aung Thiha


It would be easier if you convert the array of Strings to JSONArray, use toString and then, retrieve it by parsing first into JSONArray and then, convert it into array of Strings

like image 5
jiahao Avatar answered Sep 22 '22 03:09

jiahao