Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to save string array to the database but it wont 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 cotentvalues is not able to take an argument. how do i get around that. I was think i have 7 things in states could i like have 7 sepearte string and store stuff in each and then after put all strings back into an string array or would that be bad practice?

share|improve this question
 
Why you need to save the array in the database? there must be some other good solution. –  Yaqub Ahmad Jan 29 '12 at 13:55
 
i have 7 days of the week but and it takes it in as boolean(true or false) and it NEEDS to be boolean and since you cant save boolean to database i was going to use string array but it wont let me because of cotentvalues any ideas on what i can do? –  user1175899 Jan 29 '12 at 13:59
 
You can store the Boolean values as integers 0 (false) and 1 (true). –  Yaqub Ahmad Jan 29 '12 at 14:03
 
You could use an integer value to store the flag values. E.g. for Sunday and Tuesday the value would be 01010000 (binary), or something like that. With that said, you should really be storing the seven booleans in separate columns instead. –  dmon Jan 29 '12 at 16:05
add comment

2 Answers

up vote 12 down vote accepted

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;
}
share|improve this answer
 
Thanks so much for the answer Muhammad :) –  user1175899 Jan 29 '12 at 14:17
 
It is the same thing. When you will concatinate String with boolean. Boolean will automatically be converted to String. When you will retrieve back the Array of Booleans you have to type cast each item into Boolean by Boolean.parseBoolean(str[i]). –  Muhammad Nabeel Arif Jan 29 '12 at 14:31
 
ok thanks a lot im just new to android –  user1175899 Jan 29 '12 at 14:35
 
@MuhammadNabeelArif hey i am getting checkbox checked value in array.now i want to store it in database but different row.so how is it posible? for ex: [1,2,4] this is my array nd i want to store it in like: 1 then new row and store 2 then new row and 4.how is it posible?i am new in android so help –  Google Jan 1 '13 at 6:01
 
It is simple, consider each record 1,2,4 etc individual entry, now prepare a row for each of these and insert it into db. You need to understand basics of db. Here is a quick tutorial ' androidhive.info/2011/11/android-sqlite-database-tutorial ' –  Muhammad Nabeel Arif Jan 1 '13 at 7:21
show 2 more comments

It looks like your database is not designed as it should be. If you want to store the boolean data in SQLite database you can store the Boolean values as integers 0 (false) and 1 (true)

share|improve this answer
 
yai can store as 0, 1 but since there more than one i have to use int array and contentvalues does not allow that anyway around that? than having to make new int for each one –  user1175899 Jan 29 '12 at 14:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.