<?xml version="1.0" encoding="windows-1252"?>
<node id="3721" title="Re: Storing Objects in a Database" created="2000-02-18 20:33:27" updated="2005-08-11 14:15:55">
<type id="11">
note</type>
<author id="2675">
btrott</author>
<data>
<field name="doctext">
Have you looked at Storable? It may help you.&lt;p&gt;

It can store objects to the filesystem or to memory, then
retrieve them, transforming them back into objects.&lt;p&gt;

So perhaps you could freeze the object to a scalar, store
it in a blob in your database, pull it back out, thaw it,
then use it again? I don't know--I've only tested Storable
using the filesystem, but it seems logical that you could
do the same thing w/ a database.&lt;p&gt;

You might be able to do something like this (untested):

&lt;code&gt;
use Storable qw/freeze thaw/;
use DBI;
use Foo;    # replace this with your module name

my $object = new Foo;

my $dbh = DBI-&gt;connect('bar', 'foo', 'foo', 'Oracle')
    or die "Can't connect: ", DBI-&gt;errstr;

# store it
my $sth = $dbh-&gt;prepare(&lt;&lt;SQL) or die "Can't prepare: ", $dbh-&gt;errstr;
insert into objects (id, content)
values (?, ?)
SQL

$sth-&gt;execute("foo", freeze $object)
    or die "Can't execute: ", $dbh-&gt;errstr;

$sth-&gt;finish;


# now get it back
my $retrieve = $dbh-&gt;prepare(&lt;&lt;SQL) or die "Can't prepare: ", $dbh-&gt;errstr;
select content
from objects
where id = ?
SQL

$retrieve-&gt;execute("foo");
my $content = $retrieve-&gt;fetchrow;

my $unthawed = thaw $content;
&lt;/code&gt;

...and now use $unthawed just as you would your original
object.&lt;p&gt;

Does this sound like what you want?</field>
<field name="root_node">
3711</field>
<field name="parent_node">
3711</field>
</data>
</node>
