chickenbeef has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Bear with me as I'm very new to PERL (Literally only a day of experience).

I'm trying to read a PDF file from an IMAGE datatype column in Sybase and then save the file to disk on the server.

I've looked at the Sybase documentation on syb_ct_data_info and syb_ct_get_data but I don't seem to be getting anywhere as I'm finding it to be a very confusing process.

This is what I have so far -

my $sth = $dbp->prepare("SELECT pdf_report FROM report_table WHERE rep +ort_name = ($report_name_here)"); $sth->{syb_binary_images} = 1; $sth->{syb_no_bind_blob} = 1; $sth->execute(); while (my @row = $sth->fetchrow_array) { my $image = $sth->syb_ct_get_data(1, \$pdf_report, 0); #This just prints out the size of the file, how can I actually retr +ieve the file and write it to disk? print "$image\n"; }
  • Comment on Sybase DBD and Perl - Retrieving images from the DB and saving to disk
  • Download Code

Replies are listed 'Best First'.
Re: Sybase DBD and Perl - Retrieving images from the DB and saving to disk
by nikosv (Deacon) on Oct 16, 2015 at 08:56 UTC
    "syb_ct_get_data() returns the number of bytes that were fetched" so that is what gets assigned to $image. the image's actual data will be in \$pdf_report also change
    while (my @row = $sth->fetchrow_array)
    to
    while($row = $sth->fetchrow_arrayref) {
      Thanks, I know that fetchrow_arrayref is sometimes faster but I'll only be retrieving 12 records from the database. Changing to fetchrow_arrayref also sends the program into an infinite loop.

      Thanks for the info on \$pdf_report , now I feel as if I'm getting somewhere :)
Re: Sybase DBD and Perl - Retrieving images from the DB and saving to disk
by Discipulus (Canon) on Oct 16, 2015 at 09:04 UTC
    i can only give you a gift as welcome to the monastery: DBI recipes and remember to use everytime placeholders

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Sybase DBD and Perl - Retrieving images from the DB and saving to disk
by Anonymous Monk on Oct 16, 2015 at 12:40 UTC

    Update: I managed to get my script to do what I need it to. Thanks so much for all your help!

    #Loop through the records while (@row = $sth->fetchrow_array) { #Loop through binary data and write to a file at 10KB chunks. while(1) { $len = $sth->syb_ct_get_data(3, \$imgchunk, 10240); print "Size: $len\n"; # print "$imgchunk \n"; open FILE, "+>>something.pdf" or die $!; print FILE $imgchunk; close FILE; last if $len != 10240; }

Re: Sybase DBD and Perl - Retrieving images from the DB and saving to disk
by Anonymous Monk on Oct 16, 2015 at 09:30 UTC

      Actually, the spammers are to blame that we can't have nice things on the internet. I've whitelisted http://infocenter.sybase.com/ now.

      Update: The URLs above have been turned into links now.

      Actually DBD::Sybase already deals with the text pointers itself, so never mind my previous reply imploring the "need to use" (read|write)text functions.