DBI and variables

Chris Radcliff chris at velocigen.com
Tue May 30 15:18:19 CDT 2000


~sdpm~

Ken Humpherys wrote:
> Can the DBI module support a variable in the update statement?  If not how
> can I get around this?
> 

DBI supports variables in a number of ways. If you're only using the
update statement once, you can specify it in the normal Perl way:

<perl>
my ($f1value, $f2value, $idvalue) = (15, 20, 12);
my $statement = "UPDATE blah_table 
        SET field1 = $f1value, field2 = $f2value 
        WHERE idfield = $idvalue";
my $sth = $dbh->prepare($statement);
my $rv = $sth->execute;
</perl>

Alternately, if you're using the UPDATE statement many times, you can
use placeholders in the statement then fill them with variables later:

<perl>
my $statement = "UPDATE blah_table 
         SET field1 = ?, field2 = ? 
         WHERE idfield = ?";
my $sth = $dbh->prepare($statement);

foreach my $f1value (15..25) 
{
  my $f2value = $f1value + 5;
  my $idvalue = $f1value - 3;
  my $rv = $sth->execute($f1value, $f2value, $idvalue);
}
</perl>

Note that DBI uses ? for placeholders, and the values have to be
specified in order, one per question mark.

~chris
~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list