in reply to postgresql: store binary data?

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;

Replies are listed 'Best First'.
Re:^2 postgresql: store binary data?
by ph0enix (Friar) on Dec 02, 2002 at 14:40 UTC

    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