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?
android android-sqlite
add a comment |
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?
android android-sqlite
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
2
Another approach may be using JSONObject as described [here]( stackoverflow.com/questions/5703330/…)
– Dexter
Mar 2 '14 at 6:39
add a comment |
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?
android android-sqlite
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?
android android-sqlite
android android-sqlite
edited Jan 7 at 6:31
jkdev
5,14753364
5,14753364
asked Jan 29 '12 at 13:53
user1175899user1175899
158126
158126
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
2
Another approach may be using JSONObject as described [here]( stackoverflow.com/questions/5703330/…)
– Dexter
Mar 2 '14 at 6:39
add a comment |
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
2
Another approach may be using JSONObject as described [here]( stackoverflow.com/questions/5703330/…)
– Dexter
Mar 2 '14 at 6:39
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
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
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 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
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
2
2
Another approach may be using JSONObject as described [here]( stackoverflow.com/questions/5703330/…)
– Dexter
Mar 2 '14 at 6:39
Another approach may be using JSONObject as described [here]( stackoverflow.com/questions/5703330/…)
– Dexter
Mar 2 '14 at 6:39
add a comment |
5 Answers
5
active
oldest
votes
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;
}
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
3
this wont work if the any of the elements of the array contain a ','
– pellucide
Nov 24 '13 at 8:29
3
This column would be rather hard to query right?
– Anthony
Apr 27 '15 at 23:19
|
show 3 more comments
Based on Muhammed's answer but handles List
of String
s instead of array and uses StringBuilder
instead of allocating many String
s 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));
}
add a comment |
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);
}
It might be late but ... I can't think of a downside to this lazy method
– Lobstw
Jul 3 '16 at 1:59
@Lobstw it could lead to unexpected behavior if one of the elements ofcontent
happened to be"#a1r2ra5yd2iv1i9der"
. This would be serialized, but upon deserialization, the ambiguity would result in two empty string elements in place of the offending one string element. If other areas of the program have strict expecations of the array size, this could lead to undesired behavior.
– snapfractalpop
May 14 '17 at 10:15
@snapfractalpop yeah that's right but making the divider a long alphanumeric string would avoid this behaviour in ~100% of cases
– Lobstw
May 15 '17 at 0:56
1
@Lobstw in most cases, I agree. I comment to note that the context for these 0.001% cases can be important. For example, if this leads to some bug that then leads to some kind of security exploit in other areas of code that rely on faulty guarantees, the 0.001% can quickly grow to include script kiddies and such.
– snapfractalpop
May 15 '17 at 2:06
add a comment |
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
This is a better method than the accepted answer because you will never be bitten by the separator string (like__,__
or#a1r2ra5yd2iv1i9der
etc.) found in one of the array elements
– Alexander Farber
Sep 4 '17 at 14:11
add a comment |
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)
1
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 a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9053685%2fandroid-sqlite-saving-string-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
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
3
this wont work if the any of the elements of the array contain a ','
– pellucide
Nov 24 '13 at 8:29
3
This column would be rather hard to query right?
– Anthony
Apr 27 '15 at 23:19
|
show 3 more comments
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;
}
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
3
this wont work if the any of the elements of the array contain a ','
– pellucide
Nov 24 '13 at 8:29
3
This column would be rather hard to query right?
– Anthony
Apr 27 '15 at 23:19
|
show 3 more comments
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;
}
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;
}
edited Nov 24 '13 at 12:06
answered Jan 29 '12 at 14:15
Muhammad Nabeel ArifMuhammad Nabeel Arif
15.6k84366
15.6k84366
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
3
this wont work if the any of the elements of the array contain a ','
– pellucide
Nov 24 '13 at 8:29
3
This column would be rather hard to query right?
– Anthony
Apr 27 '15 at 23:19
|
show 3 more comments
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
3
this wont work if the any of the elements of the array contain a ','
– pellucide
Nov 24 '13 at 8:29
3
This column would be rather hard to query right?
– Anthony
Apr 27 '15 at 23:19
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
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
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
Jan 1 '13 at 6:01
@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
Jan 1 '13 at 6:01
3
3
this wont work if the any of the elements of the array contain a ','
– pellucide
Nov 24 '13 at 8:29
this wont work if the any of the elements of the array contain a ','
– pellucide
Nov 24 '13 at 8:29
3
3
This column would be rather hard to query right?
– Anthony
Apr 27 '15 at 23:19
This column would be rather hard to query right?
– Anthony
Apr 27 '15 at 23:19
|
show 3 more comments
Based on Muhammed's answer but handles List
of String
s instead of array and uses StringBuilder
instead of allocating many String
s 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));
}
add a comment |
Based on Muhammed's answer but handles List
of String
s instead of array and uses StringBuilder
instead of allocating many String
s 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));
}
add a comment |
Based on Muhammed's answer but handles List
of String
s instead of array and uses StringBuilder
instead of allocating many String
s 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));
}
Based on Muhammed's answer but handles List
of String
s instead of array and uses StringBuilder
instead of allocating many String
s 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));
}
edited Nov 15 '18 at 17:04
Miha_x64
2,13611837
2,13611837
answered Jun 22 '15 at 21:18
Nati DyksteinNati Dykstein
1,027817
1,027817
add a comment |
add a comment |
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);
}
It might be late but ... I can't think of a downside to this lazy method
– Lobstw
Jul 3 '16 at 1:59
@Lobstw it could lead to unexpected behavior if one of the elements ofcontent
happened to be"#a1r2ra5yd2iv1i9der"
. This would be serialized, but upon deserialization, the ambiguity would result in two empty string elements in place of the offending one string element. If other areas of the program have strict expecations of the array size, this could lead to undesired behavior.
– snapfractalpop
May 14 '17 at 10:15
@snapfractalpop yeah that's right but making the divider a long alphanumeric string would avoid this behaviour in ~100% of cases
– Lobstw
May 15 '17 at 0:56
1
@Lobstw in most cases, I agree. I comment to note that the context for these 0.001% cases can be important. For example, if this leads to some bug that then leads to some kind of security exploit in other areas of code that rely on faulty guarantees, the 0.001% can quickly grow to include script kiddies and such.
– snapfractalpop
May 15 '17 at 2:06
add a comment |
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);
}
It might be late but ... I can't think of a downside to this lazy method
– Lobstw
Jul 3 '16 at 1:59
@Lobstw it could lead to unexpected behavior if one of the elements ofcontent
happened to be"#a1r2ra5yd2iv1i9der"
. This would be serialized, but upon deserialization, the ambiguity would result in two empty string elements in place of the offending one string element. If other areas of the program have strict expecations of the array size, this could lead to undesired behavior.
– snapfractalpop
May 14 '17 at 10:15
@snapfractalpop yeah that's right but making the divider a long alphanumeric string would avoid this behaviour in ~100% of cases
– Lobstw
May 15 '17 at 0:56
1
@Lobstw in most cases, I agree. I comment to note that the context for these 0.001% cases can be important. For example, if this leads to some bug that then leads to some kind of security exploit in other areas of code that rely on faulty guarantees, the 0.001% can quickly grow to include script kiddies and such.
– snapfractalpop
May 15 '17 at 2:06
add a comment |
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);
}
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);
}
answered Oct 12 '14 at 21:44
Aung ThihaAung Thiha
5501620
5501620
It might be late but ... I can't think of a downside to this lazy method
– Lobstw
Jul 3 '16 at 1:59
@Lobstw it could lead to unexpected behavior if one of the elements ofcontent
happened to be"#a1r2ra5yd2iv1i9der"
. This would be serialized, but upon deserialization, the ambiguity would result in two empty string elements in place of the offending one string element. If other areas of the program have strict expecations of the array size, this could lead to undesired behavior.
– snapfractalpop
May 14 '17 at 10:15
@snapfractalpop yeah that's right but making the divider a long alphanumeric string would avoid this behaviour in ~100% of cases
– Lobstw
May 15 '17 at 0:56
1
@Lobstw in most cases, I agree. I comment to note that the context for these 0.001% cases can be important. For example, if this leads to some bug that then leads to some kind of security exploit in other areas of code that rely on faulty guarantees, the 0.001% can quickly grow to include script kiddies and such.
– snapfractalpop
May 15 '17 at 2:06
add a comment |
It might be late but ... I can't think of a downside to this lazy method
– Lobstw
Jul 3 '16 at 1:59
@Lobstw it could lead to unexpected behavior if one of the elements ofcontent
happened to be"#a1r2ra5yd2iv1i9der"
. This would be serialized, but upon deserialization, the ambiguity would result in two empty string elements in place of the offending one string element. If other areas of the program have strict expecations of the array size, this could lead to undesired behavior.
– snapfractalpop
May 14 '17 at 10:15
@snapfractalpop yeah that's right but making the divider a long alphanumeric string would avoid this behaviour in ~100% of cases
– Lobstw
May 15 '17 at 0:56
1
@Lobstw in most cases, I agree. I comment to note that the context for these 0.001% cases can be important. For example, if this leads to some bug that then leads to some kind of security exploit in other areas of code that rely on faulty guarantees, the 0.001% can quickly grow to include script kiddies and such.
– snapfractalpop
May 15 '17 at 2:06
It might be late but ... I can't think of a downside to this lazy method
– Lobstw
Jul 3 '16 at 1:59
It might be late but ... I can't think of a downside to this lazy method
– Lobstw
Jul 3 '16 at 1:59
@Lobstw it could lead to unexpected behavior if one of the elements of
content
happened to be "#a1r2ra5yd2iv1i9der"
. This would be serialized, but upon deserialization, the ambiguity would result in two empty string elements in place of the offending one string element. If other areas of the program have strict expecations of the array size, this could lead to undesired behavior.– snapfractalpop
May 14 '17 at 10:15
@Lobstw it could lead to unexpected behavior if one of the elements of
content
happened to be "#a1r2ra5yd2iv1i9der"
. This would be serialized, but upon deserialization, the ambiguity would result in two empty string elements in place of the offending one string element. If other areas of the program have strict expecations of the array size, this could lead to undesired behavior.– snapfractalpop
May 14 '17 at 10:15
@snapfractalpop yeah that's right but making the divider a long alphanumeric string would avoid this behaviour in ~100% of cases
– Lobstw
May 15 '17 at 0:56
@snapfractalpop yeah that's right but making the divider a long alphanumeric string would avoid this behaviour in ~100% of cases
– Lobstw
May 15 '17 at 0:56
1
1
@Lobstw in most cases, I agree. I comment to note that the context for these 0.001% cases can be important. For example, if this leads to some bug that then leads to some kind of security exploit in other areas of code that rely on faulty guarantees, the 0.001% can quickly grow to include script kiddies and such.
– snapfractalpop
May 15 '17 at 2:06
@Lobstw in most cases, I agree. I comment to note that the context for these 0.001% cases can be important. For example, if this leads to some bug that then leads to some kind of security exploit in other areas of code that rely on faulty guarantees, the 0.001% can quickly grow to include script kiddies and such.
– snapfractalpop
May 15 '17 at 2:06
add a comment |
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
This is a better method than the accepted answer because you will never be bitten by the separator string (like__,__
or#a1r2ra5yd2iv1i9der
etc.) found in one of the array elements
– Alexander Farber
Sep 4 '17 at 14:11
add a comment |
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
This is a better method than the accepted answer because you will never be bitten by the separator string (like__,__
or#a1r2ra5yd2iv1i9der
etc.) found in one of the array elements
– Alexander Farber
Sep 4 '17 at 14:11
add a comment |
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
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
answered Aug 29 '15 at 17:24
jiahaojiahao
2,71422834
2,71422834
This is a better method than the accepted answer because you will never be bitten by the separator string (like__,__
or#a1r2ra5yd2iv1i9der
etc.) found in one of the array elements
– Alexander Farber
Sep 4 '17 at 14:11
add a comment |
This is a better method than the accepted answer because you will never be bitten by the separator string (like__,__
or#a1r2ra5yd2iv1i9der
etc.) found in one of the array elements
– Alexander Farber
Sep 4 '17 at 14:11
This is a better method than the accepted answer because you will never be bitten by the separator string (like
__,__
or #a1r2ra5yd2iv1i9der
etc.) found in one of the array elements– Alexander Farber
Sep 4 '17 at 14:11
This is a better method than the accepted answer because you will never be bitten by the separator string (like
__,__
or #a1r2ra5yd2iv1i9der
etc.) found in one of the array elements– Alexander Farber
Sep 4 '17 at 14:11
add a comment |
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)
1
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 a comment |
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)
1
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 a comment |
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)
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)
answered Jan 29 '12 at 14:05
Yaqub AhmadYaqub Ahmad
23.2k2086133
23.2k2086133
1
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 a comment |
1
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
1
1
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
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 a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9053685%2fandroid-sqlite-saving-string-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
2
Another approach may be using JSONObject as described [here]( stackoverflow.com/questions/5703330/…)
– Dexter
Mar 2 '14 at 6:39