Simple DBI Examples

1. Opening a Database Handle

$dbh = DBI->connect('dbi:mysql:www','chef','grok',{PrintError=>0});
$dbh->disconnect;  # (when done) 

2. Executing a Simple SQL Command

$count = $dbh->do('UPDATE ProductList SET SET price=price-(price*0.20)');
die $dbh->errstr unless defined $count;

3. Executing a SELECT Statement

$sth = $dbh->prepare('SELECT catalog,name FROM ProductList') 
       || die $dbh->errstr;
$sth->execute() || die $sth->errstr;
while (my ($catalog,$name) = $sth->fetchrow_array) {
    print "catno => $catalog, name => $name\n";
}
$sth->finish;

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Sun Apr 25 14:44:09 EDT 1999