tech_learner
tech_learner

Reputation: 725

Storing 2-dimensional string array into the SQLite database

I wish to store the values in a string array i.e. values of arr[16][16]. If I put this using a 2 for loop, as per my understanding 256 rows would be added to the database which is not my requirement. I want all the 256 values in one column of a single row in database.

I am new to android. Please suggest.

Upvotes: 1

Views: 1868

Answers (2)

eternalmatt
eternalmatt

Reputation: 3564

Could you do something like:

StringBuilder one_big_row = new StringBuilder();
for(String[] array : arr)
     for(String string : array)
          one_big_row.append(string);
String row = one_big_row.toString();

Seems kinda silly to me. Edit: should use StringBuilder for performance reasons.

Also, if you don't need this data to persist (after user closes the app), then you probably shouldn't use the SQLite DB. If you do need it to persist, try to rethink how it should be stored, because it will be tedious and slow when you have retrieve this data via substrings of a very large String consisting of 256 other strings.

Upvotes: 3

Damp
Damp

Reputation: 3348

You could serialize the array and insert the serialized data in a BLOB column. Then you'd just have to retrieve your BLOB value and de-serialize to get your array back.

Upvotes: 3

Related Questions