Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

store/retrieve data structure in MySQL

by BarMeister (Beadle)
on Jul 31, 2005 at 20:25 UTC ( #479735=perlquestion: print w/replies, xml ) Need Help??
BarMeister has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if what I am attempting is even possible...I have seen a few references to it while searching but haven't found a definitive answer yet.

I am trying to insert/update/retrieve a data structure in MySQL. My question is what do I pass in as the value when using bind_param? I am inserting it into a BLOB data type. The data structure is an array of objects (hash refs).

So is it possible to do this or do I have to serialize/deserialze the structure first?


Replies are listed 'Best First'.
Re: store/retrieve data structure in MySQL
by friedo (Prior) on Jul 31, 2005 at 21:18 UTC
    You will need to serialize the structure in order to save it into the DB. There are many options available, each with advantages and disadvantages. Data::Dumper is human-readable and serializes your structure as Perl code, YAML is human-readable and cross platform (there are parsers for several languages). There is also Storable, which is a binary format and is somewhat faster. And of course there are various XML things to choose from.
Re: store/retrieve data structure in MySQL
by rnahi (Curate) on Jul 31, 2005 at 21:33 UTC

    You may use Apache::Session::MySQL.

    Despite the name, and despite being designed for using with mod_perl, you can use it even in a non-web application. Or, as the docs say

    it can also be used for any situation where data persistence is desirable.

    It's a ready to use framework. Look at the docs. It's quite easy to store a data structure into a database table, and even to use it with a tied hash.

      Note, it can be a bad idea to do this, instead of having a proper db design.

      I have seen a coworker just use Data::Dumper to store text repsentations of objects. He then ends up using Regexp queries to search within db fields for object attributes. So he is storing more information than required and losing many rdbms features.

      Tying your data to your dev language is obviously not a great idea for portability either. Anyway you dump it ,then you eval it as pseudo below. You will probably have to use a string rather than binary representation if you wish to search the data easily. As you are using a blob field you will have to use storable instead.

      #get string representation for insert into db my $data={}; use Data::Dumper; my $serial=Dumper($data); #get object back from string my $obj=eval $serial;
        I store all the properties of the object within the database in a nice normalized structure....this was only needed for use within a summary table for optimization reasons. I build the objects rarely so why not cache them? :-)

        Thanks everyone for the help.

Re: store/retrieve data structure in MySQL
by tphyahoo (Vicar) on Aug 03, 2005 at 08:38 UTC