Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Storing integer arrays into MySQL databse

Options
  • 28-02-2004 3:28pm
    #1
    Closed Accounts Posts: 364 ✭✭


    Hi

    Does anyone know is it possible to store an array of integer into a MySQL database table? I have a table in MySQL and it consists of varchars and ints, but I want to store an array of integers also.

    Can this be done in the conventional way, or will I need nested tables ??


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I have some sort of recollection of an Object hash in Java that reduces an Object to a hashed value, which can then be used to regenerate the Object at a later date. My details are sketchy because it's been a while since I heard of it.

    I don't know if it will work for arrays, or even it it works at all. But if it does, that may be what you're looking for. Obviously the only constraint is that the hash value would mean nothing to anything but a Java program, so you would be unable to retreive any of the values in the array except through a Java-based interface.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Turns out I'm not as thought as I crazy I was ;)

    Do a search and read up on Object Serialization in Java which basically allows you to turn an Object in a raw byte stream, and back again quite quickly.

    It's a tad too detailed to go into here, but it should serve your purposes perfectly.


  • Closed Accounts Posts: 364 ✭✭Matfinn


    Seamus,

    Thanks for all the help, well appreciated. Heres one idea I did have. If I created a database table in mysql and stored the ints as a string, Java could convert to a String and back again quite easily.

    something like this
    
    int [] numbers = new int[10];
    String numberString = "";
    for(int i =0; i < numbers.length; i++)
    {
          numberString = numberString + numbers[i] + ",";
    }
    //to get rid of the trailing comma at the end
    numberString = numberString.subString(0, numberString.length() - 1);
    
    try
    {
          Statement stmt = connection.createStatement();
          stmt.executeUpdate(insert into tableName values (....... ));
    }
    catch
    {
          code to catch exception........
    }
    
    

    For anyone who needs it.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    That should do fine, coupled with a StringTokenizer to parse back the values again.

    The only thing is the size of the array. Once you start getting into big arrays of ints, the size of the String in the database may become cumbersome, and the overhead from parsing it back again may be huge.

    But for small arrays, that seems like a much simpler solution. :)


Advertisement