Re: lo_import for storing Blobs - Mailing list pgsql-sql

From pgsql-sql
Subject Re: lo_import for storing Blobs
Date
Msg-id fc.000f567200a0b449000f567200a0b449.a0b462@fc.emc.com.ph
Whole thread Raw
In response to lo_import for storing Blobs  (Laurent <laurent@presenceweb.com>)
List pgsql-sql
You can use 'DBI'

from test.pl of DBD::Pg

# create large object from binary file

my ($ascii, $pgin);
foreach $ascii (0..255) {   $pgin .= chr($ascii);
};

my $PGIN = '/tmp/pgin';
open(PGIN, ">$PGIN") or die "can not open $PGIN";
print PGIN $pgin;
close PGIN;

# begin transaction
$dbh->{AutoCommit} = 0;

my $lobjId;
( $lobjId = $dbh->func($PGIN, 'lo_import') )   and print "\$dbh->func(lo_import) ...... ok\n"   or  print
"\$dbh->func(lo_import)...... not ok\n";
 

# end transaction
$dbh->{AutoCommit} = 1;

unlink $PGIN; 

or you can use 'Perl5 extension for PostgreSQL' ...   note: i didn't test the script

use strict;
use Pg;   

my $dbname = 'your dbname';
my $lo_path = 'path/to/you/binaryfile';
my ($tbl, $fld) = ('your table', 'oid field');

my $conn = Pg::connectdb("dbname=$dbname");
die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status;   
 my $result = $conn->exec("BEGIN"); die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus;
 # import  large object and get its oid my $new_oid = $conn->lo_import($lo_path) or die $conn->errorMessage;
 $result = $conn->exec("END"); die $conn->errorMessage unless PGRES_COMMAND_OK eq
$result->resultStatus; 

# insert the oid of the lobj my $sql = sprintf("INSERT INTO %s (%s) VALUES (%ld)",   $tbl, $fld, $new_oid); $result =
$conn->exec($sql);die $conn->errorMessage unless PGRES_COMMAND_OK eq
 
$result->resultStatus;  

undef $conn;     


Sherwin

laurent@presenceweb.com writes:
>I need to store a binary file in a database. I use a cgi writed in shell
>to do it. So I can use Postgres user to execute the cgi.
>
>How can I store a binary file in a database with a cgi ?
>
>Thanks a lot.
>
>Laurent.
>
>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>



pgsql-sql by date:

Previous
From: Jan Wieck
Date:
Subject: Re: Help creating rules/triggers/functions
Next
From: Richard Huxton
Date:
Subject: Re: Insert into VIEW ???