| From: | "pgsql-sql" <pgsql-sql(at)fc(dot)emc(dot)com(dot)ph> | 
|---|---|
| To: | laurent(at)presenceweb(dot)com | 
| Cc: | pgsql-sql(at)postgresql(dot)org | 
| Subject: | Re: lo_import for storing Blobs | 
| Date: | 2001-03-03 09:33:41 | 
| Message-ID: | fc.000f567200a0b449000f567200a0b449.a0b462@fc.emc.com.ph | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | 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(at)presenceweb(dot)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(at)postgresql(dot)org)
>
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Richard Huxton | 2001-03-03 15:13:25 | Re: Insert into VIEW ??? | 
| Previous Message | Tom Lane | 2001-03-03 06:44:39 | Re: Help needed -> ERROR: record arow has no field description |