echoangel911 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I was wondering if it is somehow possible to store hash information into mySQL. For example, if you have
$hash = { 'day 1' => ['apple','oranges','peaches'], 'day 2' => ['watermelon','banana','stones'] };
is it possible to store in mySQL and how would it look like in terms of a table? thanks

Replies are listed 'Best First'.
Re: storing hash data into tables for mySQL??
by jettero (Monsignor) on Jan 04, 2007 at 21:00 UTC

    Depending on my mood, I've been known to use anything from Data::Dumper to Storable to store that sort of data in mysql. The cool people probably use DBomb or DBIx::Class to store it more stylistically and tablemagically. It all depends on your goals. Your choices are definitely not limited to anything I posted here. What are your goals for this storage system?

    UPDATE: Concerning OOPS below. It appears it does support mysql under inno now. This is hardly the place to discuss it, but I wonder if there's a better place in the CPAN namespaces for both DBomb and OOPS. They're both pretty cool, but I seem to recall it's poor form to dump things into the root namespace like that.

    -Paul

Re: storing hash data into tables for mySQL??
by roboticus (Chancellor) on Jan 04, 2007 at 22:29 UTC
    Yes, it's possible.

    But there are numerous ways to do it, and numerous ways to structure the data tables. You probably ought to decide what it is you *want*.

    First, figure out what you're going to do with the data when it's in the database (i.e., what questions are you going to ask?).

    Next, design a table structure that makes your questions easy and efficient to answer. Note that you'll have to balance that with the amount and frequency of adding additional data to the tables. (Frequent large updates means you'll want to go easy on the indexes, while infrequent updates mean you can add plenty of indexes to make make it easier to ask your questions.

    Then, and only then worry about the code.

    --roboticus

Re: storing hash data into tables for mySQL??
by SheridanCat (Pilgrim) on Jan 04, 2007 at 22:54 UTC
    You might want to consider using YAML for this. YAML will allow you to serialize your data structures in a text format that is human readable. You would then just insert/update into a table as you would any data.

    The upside of using YAML is that it's portable. Insert the data with Perl and retrieve it with Ruby (or whatever) or just look at it from the mysql console.

Re: storing hash data into tables for mySQL??
by Arunbear (Prior) on Jan 04, 2007 at 21:55 UTC
    OOPS can do that (also supports SQLite and PostgreSQL).