pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
lo_import for storing Blobs
2+ messages / 2 participants
[nested] [flat]

* lo_import for storing Blobs
@ 2001-03-02 10:34 Laurent <laurent@presenceweb.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Laurent @ 2001-03-02 10:34 UTC (permalink / raw)
  To: pgsql-sql

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.






^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: lo_import for storing Blobs
@ 2001-03-03 09:33 pgsql-sql <pgsql-sql@fc.emc.com.ph>
  0 siblings, 0 replies; 2+ messages in thread

From: pgsql-sql @ 2001-03-03 09:33 UTC (permalink / raw)
  To: laurent@presenceweb.com; +Cc: 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)
>




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-03-03 09:33 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-03-02 10:34 lo_import for storing Blobs Laurent <laurent@presenceweb.com>
2001-03-03 09:33 Re: lo_import for storing Blobs pgsql-sql <pgsql-sql@fc.emc.com.ph>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox