#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("DBI:mysql:test:192.168.1.2", 'root', 'password');
my $sth = $dbh->prepare("SELECT * FROM test1");
$sth->execute();
while ( my @row = $sth->fetchrow_array() )
{
print join('\t', @row)."\n";
}
$sth->finish();
$dbh->disconnect();
my $dbh = DBI->connect("DBI:mysql:test:192.168.1.2", 'root', 'password');
DBI->connect
my $sth = $dbh->prepare("SELECT * FROM test1");
$sth->execute();
$dbh->do("UPDATE test1 SET time=now()");
$dbh->do(SQL语句)
my $res_operator = $dbhandle->prepare( qq{
SELECT o_customerid, COUNT(*) AS totalMsgNum FROM mm4fcdrs
WHERE (m_date>'$begindate') AND (m_date<'enddate')
GROUP BY o_customerid
});
INSERT INTO test1 VALUES (NULL, ‘a’, ‘2005-04-01’)
... ...
INSERT INTO test1 VALUES (NULL, ‘z’, ‘2005-04-01’)
my $sth = $dbh->prepare( qq{
INSERT INTO test1 VALUES (NULL, ?, ‘2005-04-01’)
} );
for my $value('a'..'z') {
$sth->execute($value);
}
$sth->fetchrow_array()
while ( my @row = $sth->fetchrow_array() ) {
print "$row[0], $row[1], $row[2]\n";
}
while ( my ($id, $name, $time) = $sth->fetchrow_array() ) {
print "$id, $name, $time\n";
}
fetchrow_array
fetchrow_arrayref
while ( my $row_ref = $sth->fetchrow_arrayref() ) {
for (my $i = 0; $i < @{$row_ref}; $i++) {
print "$row_ref->[$i]\t";
}
print "\n";
}
@{$row_ref}
->
while ( my $record = $sth->fetchrow_hashref() ) {
for my $field( keys %{$record} ) {
print "$field: $record->{$field}\t";
}
print "\n";
}