1

I want to save an array of strings that I have in my java program to a mysql table field or column. What is the best way to save this array knowing that you can't save an array as a one column in the database table. I know that it's better to use normalization but I need to save the array into a one column and not into individual columns. Is it a good idea to concatenate all the elements of the array into one string and save it into the table as a Text field???.... Thanks for your help !!!!!!

niz
  • 19
  • 1
  • 7

1 Answers1

1

Is it a good idea to concatenate all the elements of the array into one string and save it into the table as a Text field???

It highly depends on what usage this string array has. If you won't be concatenating and splitting very often, it's ok. IMHO you can do this and observe the performance. If the performance is not good, then consider another approach, like:

  • Serialize the array (store as BLOB or encode as string to store in a VARCHAR/TEXT)
  • GSON (this will allow you to keep the column type as VARCHAR or TEXT)
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • Thanks for your fast reply.... Well my array is an array of strings containing the urls of images..... I have about a 1000 row and for each row I have 30 or 40 images.... So I'll be concatenating 40 or 50 urls corresponding to images.... Can you please provide additional details concerning serializing the array and GSON? Can you provide an example? Thx again – niz Jun 27 '13 at 14:28
  • I suggest you test concatenating/splitting first. It's the simplest solution and it should be OK. If the performance is a problem, then you should look for alternative solutions. Always KISS. – m0skit0 Jun 27 '13 at 14:31