0

I've been using Cordova and the storage adapter for SQLite. When I store objects using JSON.stringify and the retrieve them they work. I also have several fields that are arrays, but when I store them they are just stored comma-delimited. If there is only one item in the array then it's just stored as a single string.

Initial Array:

["test1", "test2", "test3"]

Stored As:

test1, test2, test3

I could just split those specific fields when I retrieve them from the database, but still wondering if there is a better way to store them. Is there a elegant way to store an array and retrieve it in SQLite using JavaScript?

2 Answers 2

1

I don't see any problem ,you can access initial array after parsing (JSON.parse(yourStringifiedArrayString)).

  var yourarr=["test1", "test2", "test3"];
  var afterStringify = JSON.stringify(yourarr);
  console.log('After Stringify:'+afterStringify);
  var prevArr=JSON.parse(afterStringify);
  console.log('After Parsing:'+prevArr);
  //now access your array
   alert(prevArr[1]);//will show test2

Check Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

I guess you're right. It looks stringifying and parsing arrays works too, even if they aren't JSON. I'd imagine using split would be less expensive on the processor though?
I don't think split would be less expensive. if you are agree with my answer then you should accept it.
I just switched all of my splits to use the json functions and it seems to be working.
0

I don't see a problem with saving an array as a delimited string - It works fine for me. Just need to recreate the array using split() method when you retrieve it from SQLite.

1 Comment

Yes, I know it's something I can work around, though I have a lot of tables and fields. It just feels like a lot of extra logic to check for those fields and then split them before returning the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.