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

I have an existing DB with a column that has binary data in it. My task is to extract the data and place it into a working file. The filename is stored in another column. The following is what I have.
while ($record = $sth->fetchrow_arrayref()) { $id = $$record[0]; $data = $$record[1]; $fileName = $$record[3]; my $outFile = "$filesDir$fileName"; open OUTF, ">$outFile" or print "Can't open '$outFile' for wri +ting\n<BR>"; binmode OUTF; print OUTF $data; close OUTF; }
From the examples of binary data manipulation I have read, this should work without a problem. However, the created files never work. Does anyone have any insight as to what I am missing? Any help would be greatly appreciated. Thanks

Replies are listed 'Best First'.
Re: Extracting binary data from mssql
by fmerges (Chaplain) on Jul 25, 2007 at 21:45 UTC

    Hi,

    See DBI. Skip to section named 'Handling BLOB / LONG / Memo Fields'

    BTW, look at the way your assigning the variables, then, are you using the id? The way you're composing the filename is not safe, see File::Spec. For opening files, use a lexical variable, and use the three way open.

    Regards,

    fmerges at irc.freenode.net
Re: Extracting binary data from mssql
by runrig (Abbot) on Jul 25, 2007 at 21:48 UTC
    define "not working". File is empty? File doesn't contain what you expect? File is not created? Does the code enter the while loop at all? Not fetching any data? (update: )Are you using RaiseError?
      My guess is that the file is too short.

      The cause must be looked for in lack of proper initialization of LongReadLen.

        With that in mind, my guess is that the default of LongTruncOk being false should cause the fetch to fail (Update: Would LongReadLen being zero cause the fetch to fail, or fetch an empty string?), but RaiseError is probably false also, so we're not catching that error, though PrintError is probably true, so we should at least see the error, but the error isn't mentioned, so I'm not sure what's going on because there's too little info :-)
      The loop runs and the records are fetched. All the data appears to be coming back. The files that are created definitely have data because they differ in byte size. The only success I saw was with a .doc file. After trying to open it, I see all the contents and everything looks fine. However, gif and jpg and other files don't work. It seems like all of the data is there but something along the lines of an end of file marker is missing. Any suggestions? If you need more info, please let me know.