#/usr/bin/perl -w
use strict;
use DBI;

my $dbh = DBI->connect();
$dbh->{RaiseError} = 1;	 # raise the error
$dbh->{PrintError} = 0;	 # but don't print it.

eval {
   # if it's not already created, the eval will silently ignore this
   $dbh->do("drop table longtest;");
};

my $dbname = $dbh->get_info(17); # DBI::SQL_DBMS_NAME
print $dbname, "\n";
if ($dbname =~ /SQL Server/i) {
   $dbh->do("Create table longtest (id integer, picture image)");
} elsif ($dbname =~ /Oracle/i) {
   $dbh->do("Create table longtest (id integer, picture long)");
} elsif ($dbname =~ /Access/i) {
   $dbh->do("Create table longtest (id integer, picture longbinary)");
} else {
   die "Can't figure out correct table types\n";
}
   
my $sth = $dbh->prepare("insert into longtest (id, picture) values (?, ?)");
my $id = 0;
my $file;
foreach $file (@ARGV) {
   my $blob;
   eval {
      print "Reading: $file\n";
      $blob = readblobfile($file);
   };
   if (!$@) {
      $sth->bind_param(1, $id, DBI::SQL_INTEGER);
      $sth->bind_param(2, $blob, DBI::SQL_BINARY);
      $sth->execute;
   } else {
      printf("Couldn't read file: $@\n");
   }
   $id++;
}

$dbh->disconnect();

sub readblobfile($) {
   my $filename = shift;
   local(*FILE, $\);	# automatically close file at end of scope
   open(FILE, "<$filename") or die "Can't open file $!\n";
   binmode(FILE);
   <FILE>;
}
