Try this syntax:

my $dbh = DBI::connect("...",{RaiseError=>1}); # Add raise error to your dbh
class, verbosity is good.

my $sql = qq{insert into foo (col1,col2) values (?,?)}; # Seperate sql from
prepare.
my $sth = $dbh->prepare($sql);

foreach my $x (@ary){
  $sth->insert($x,'test');
}

On Thu, Oct 6, 2011 at 1:03 PM, Jeffrey Joh <johjeff...@hotmail.com> wrote:

>
> Thank you for your help Rob.
>
>
>
> How can I write a code to go to next iteration of the foreach loop if there
> is a "Cannot bind a reference" error?
>
>
> Thanks,
>
> Jeffrey
>
>
>
> ----------------------------------------
> > Date: Sun, 2 Oct 2011 01:54:38 +0100
> > From: rob.di...@gmx.com
> > To: beginners@perl.org
> > CC: johjeff...@hotmail.com
> > Subject: Re: Cannot bind a reference
> >
> > On 01/10/2011 23:16, Jeffrey Joh wrote:
> > >
> > > I am trying to run an insert statement with DBI.
> > >
> > > $dbh->do(q{insert into zillow_table values
> (?,?,?,?,?,?,?,?)},undef,($homeid,$code,$text,$pid,$street,$city,$state,$zlastupdated));
> > >
> > > However, I get "Cannot bind a reference" error. Why does that occur?
> > > $dbh is part of a foreach loop and several of the executed variables
> > > are null/undefined.
> >
> > Your problem is almost certainly because one of your bound variables
> >
> > $homeid
> > $code
> > $text
> > $pid
> > $street
> > $city
> > $state
> > $zlastupdated
> >
> > is set to a reference. You can only bind values that are set to a
> > string, a number, or undef. You should check the values of your
> > variables before each insert to find where it is going wrong.
> >
> > Also, if you are executing the insertion in a loop then you should first
> > prepare it outside the loop, otherwise the same SQL statement is
> > wastefully being prepared many times. Something like
> >
> > my $insert = $dbh->prepare(q{insert into zillow_table values
> (?,?,?,?,?,?,?,?)});
> > foreach (...) {
> >
> $insert->execute($homeid,$code,$text,$pid,$street,$city,$state,$zlastupdated);
> > }
> >
> > HTH,
> >
> > Rob
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to