Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl dbi urgent

Status
Not open for further replies.

bardley

Programmer
Joined
May 8, 2001
Messages
121
Location
US
It seems to me that this is a really hard problem, but I guess all problems are hard if you haven't figured out an answer.

I am using the DBI and DBD:Oracle modules, and I know I can load BLOBs into the database because the documentation says it's supported. However, the BLOBs I wish to load are jpg's and/or pdf's, and I can't figure out a way to get the data to the DBI without actually reading the file into the Perl program.

I would be fine with that, except that (I guess because of the non-ASCII format) Perl won't read in the entire file. I think it finds what look like EOF characters in the file when it's reading it and stops right in the middle (or sooner).

So, can anyone tell me
1. If there is a way to handle these formats so I can get them into an SQL insert statement within my script, or

2. If there is a way to ?parse? the raw data from a non-ASCII file so it can be entirely read into my program?

Thanks in Advance for solutions, advice, pointers, or general two cents' worth!!

bardley Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Brad, I know quite a bit about DBI and DBD::Oracle and loading text into a blob, but not about loading images into a blob. My guess is that you need to read the image file into a variable using a binary read - something like this:

my $XFER_SIZE = (32 * 1024); ### 32k

my $abs_path_to_image_file = "/path/to/image/file";
if (! open(IN_FILE,&quot;<${abs_file_to_stream}&quot;)) {
### error - do some error processing here
}

my $total_size = 0;
my $size;
my $image = &quot;&quot;;
binmode IN_FILE;
while ($size = read(IN_FILE,$buf,$XFER_SIZE)) {
$image .= $buf;
$total_size += $size;
}

============================================
Or you can try &quot;slurp&quot;ing the whole image into a scalar with one read - not sure how to do that off the top of my head. Then once you have $image you can insert that into the database. This is just a guess - I haven't tested this code at all. If this doesn't solve your problem, try searching the archives of the DBI mailing list - go to google and search for DBI Users Archive. I'm sure other people on the DBI mailing list have loaded images into Oracle blob's before, so I'm pretty sure you'll find some good info there.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Found an answer in an old help archive--has to do with setting $/ to undef. I'm working on this right now, so wish me luck. Thanks for the quick response. Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
I wasn't sure of the variable but $/ is probably the one that regulates what Perl considers to be the end-of-line. Since perl is line oriented, if you undef $/, then a read on a filehandle will read the whole file(this is called &quot;slurp&quot;ing), instead of just one line. So you probably can do:

undef $/;
$image = <IMAGE_FH>;

and then do your insert of the blob into Oracle binding the $image scalar to the blob.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Yes, $/ is the perl end-of-line character. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top