Many DBD drivers support BLOB columns natively. Most drivers/databases require you to use bind variables (which are usualy safer and often more efficient than composing quoted strings of sql code). Using bind params is not that difficult, assuming you are using mysql, and have a table named foo 'create table foo (data BLOB)', the following contrived example code should work for you:
use strict; use warnings; use DBI; my $dbh = DBI->connect('dbi:mysql:krb','mortis',''); die $DBI::errstr unless $dbh; if (@ARGV) { my $img_file = 'test.jpg'; my $img_data = getFile($img_file); my $sql = 'INSERT INTO foo (data) VALUES (?)'; unless ($dbh->do($sql,undef,$img_data)) { die "Error executing sql: '$sql' : $DBI::errstr : ",$dbh->errstr," +\n"; } print "DATA INSERTED\n"; } my $resultSet = $dbh->selectall_arrayref('SELECT * FROM foo'); my $imgData = $resultSet->[0]->[0]; writeFile('test2.jpg',$imgData); $dbh->disconnect; sub getFile { my($file) = @_; my $fh; unless (open $fh, "<", $file) { die "Error opening $file : $!\n"; } my $data; { local $/ = undef; $data = <$fh>; } close $fh; return $data; } sub writeFile { my($file,$data) = @_; my $fh; unless (open $fh, ">", $file) { die "Error opening $file : $!\n"; } print $fh $data; close $fh; }

Iirc, DBD::Oracle supports blobs natively in the same manner (you might have to pass statement handle attributes to identify the column's type though). DBD::Pg also supports blob columns, though you need to have blob access inside of a transaction, and you used to have to call some Postgresql specific DBD api to use them. Newer versions may be better.

hth,

Kyle


In reply to Re: blob field by mortis
in thread blob field by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.