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

hi monks,

I am tring to insert a picture in to a blob type and the code doesnt work... Can you all help me... Thanks in advance...

#!/usr/bin/perl #A perl script to insert a blob into a database table #TABLE: create table blob_eg(sno integer not null auto_increment, pics + blob, primary key(sno)); #Module import use strict; use DBI; use diagnostics; #varible Declaration my $dbHandle; my $stmtHandle; my $pic; my $quoted_pic; #opening a picture open(my $fh, '/home/backup/old_docs/arun/pictures/1.jpg' ) or die $!; read( $fh, $pic, -s $fh ); $dbHandle=DBI->connect("dbi:mysql:tempdb","arun","password",{ RaiseErr +or=>1, AutoCommit=>0 }) or die ("cannot connect"); $quoted_pic = $dbHandle->quote($pic); $stmtHandle = $dbHandle->prepare( qq{Insert into blob_eg('pic') values +(?)}) or die("cannot prepare\n"); eval { $stmtHandle->execute($quoted_pic); }; if($@) { print "cannot execute. exception caught... $@"; $dbHandle->rollback(); }else { $stmtHandle->finish(); $dbHandle->disconnect(); print "Successfully inserted the pic into db"; } close($fh);

Replies are listed 'Best First'.
Re: Inserting picture into blob
by marto (Cardinal) on Nov 14, 2005 at 15:06 UTC
    Hi arunvelusamy,

    Perhaps this is just a typo but in your table creat comment you have stated "pics + blob" and in your code you have ... Insert into blob_eg('pic') ....
    Failing that can you tell us what error message(s ?) you are getting?

    Hope this helps.

    Martin
Re: Inserting picture into blob
by zentara (Cardinal) on Nov 14, 2005 at 17:33 UTC
    There is a similar problem solved at Working with DBI and BLOB. I would insert some debugging statements to make sure that your $pic gets the image( maybe you need binmode on $fh ) or make sure the $quoted_pic has what you think it is. Maybe try printing $pic and $quoted_pic and see if they contain what you want.

    I'm not really a human, but I play one on earth. flash japh
Re: Inserting picture into blob
by cowboy (Friar) on Nov 14, 2005 at 22:27 UTC
    You're mixing up placeholders/quoting. If you are using placeholders in your query, do not quote the values, the placeholder takes care of that.
    Note, this probably isn't why it's failing, but it will (probably) give you a broken image if you quote, and use placeholders.

    Update: added second comment