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

Hi monks

I want to store some binary data in sql database (actually postgresql) produced by Storable::nfreeze(). How can I do it?

Test table have following structure

Attribute | Type | Modifier -----------+------------------------+---------- pkey | character varying(255) | not null pvalue | bytea | Index: demo_pkey

Following test code fails with error 'Unterminated quoted string at ...' for line with execute call.

#!/usr/bin/perl -w use strict; use Storable qw(nfreeze thaw); use DBI; use DBD::Pg; # database settings my $db_name = 'unicode'; my $db_host = 'localhost'; my $db_user = 'elric'; my $db_pass = 'test01'; my $dbh = DBI->connect("dbi:Pg:dbname=$db_name;host=$db_host;", "$db_user", "$db_pass"); my $test = { 'ahoj svete...', [ 1, 23, 'ttt' ], }; my $sth = $dbh->prepare("insert into demo values (?, ?)"); $sth->execute('test', nfreeze($test)); $dbh->disconnect(); exit 0;

I need to have similar functionality like MLDBM module, but for sql database. The modules Tie::RDBM, Tie::MLDBM looks buggy for me.

How can I solve this. What I'm doing wrong? or is there already preset other module for doing this?

Replies are listed 'Best First'.
Re: postgresql: store binary data?
by diotalevi (Canon) on Dec 02, 2002 at 14:01 UTC

    I didn't know the answer to your question so I checked in the usual places. I'm mostly telling you story of how I got the information I'm about to share since you'll need to be able to do this later on your own.

    I first checked the mailing list archives on postgresql.org (which appear to be down right now) and the google query postgresql bytea perl which returns the wonderful post Re: Escaping Binary Data in DBD::Pg. So this leads me back to DBI and it's support of data types. Reading that yields the nifty snippet perl -MDBI=:sql_types -e 'for(@{$DBI::EXPORT_TAGS{sql_types}}){printf "%s=%d\n", $_, &{"DBI::$_"}}'|grep BINARY. That also mentions to check out type_info(), type_info_all() and bind_param() for possible uses of that information.

    All that work (15 minutes actually) brings me to the following potential code snippet. If this doesn't work then follow up with that archive message I linked to. The PostgreSQL mailing list is also a good place to ask this sort of question.

    use DBI qw(:sql_types); # ... my $sth = $dbh->prepare("insert into demo values (?, ?)"); $sth->bind_param( 1, 'test', SQL_TEXT ); $sth->bind_param( 2, nfreeze($test), SQL_BINARY ); $sth->execute;
    __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

      Now I'm found following note in the DBD::Pg module documentation.

      NOTE: The undocumented (and invalid) support for the "SQL_BINARY" data type is officially deprecated. Use "PG_BYTEA" with "bind_param()" instead: $rv = $sth->bind_param($param_num, $bind_value, { pg_type => DBD::Pg::PG_BYTEA });

      Code seems to work OK after modification

      $sth->bind_param(2, nfreeze($test), { pg_type => DBD::Pg::PG_BYTEA });

      Thanks

Re: postgresql: store binary data?
by djantzen (Priest) on Dec 02, 2002 at 13:53 UTC

    I haven't used PostgreSQL and its 'bytea' type, but I think you may need to call DBI::bind_param before execute in order to tell your statement handle explicitly that you're supplying binary data.

    Try something like this before calling execute:

    $sth->bind_param(2, nfreeze($test, { TYPE => 'bytea' }));

    This may also work:

    $sth->execute(nfreeze($test), { TYPE => 'bytea' });

    Be sure to check the value of $sth->errstr, and here are a couple of related threads: Maintaining state through storable.pm? and BLOBs and error ORA-01465

    Update: fixed index of param to bind and a typo, and bytea should be SQL_BINARY like diotalevi's example.

Re: postgresql: store binary data?
by rdfield (Priest) on Dec 02, 2002 at 13:50 UTC
    My Storable documentation doesn't mention nfreeze. Update: Yes it does. Ho-hum.

    rdfield