[perl]代码库
#-----------------------------
use DBI;
$dbh = DBI->connect ( 'DBI:driver:database', 'username', 'auth',
{ RaiseError => 1, AutoCommit => 1} );
$dbh->do ( $sql );
$sth = $dbh->prepare ( $sql );
$sth->execute();
while ( @row = $sth->fetchrow_array )
{
# ...
}
$sth->finish();
$dbh->disconnect();
#-----------------------------
#disconnect(DBI::db=HASH(0x9df84)) invalidates 1 active cursor(s)
# at -e line 1.
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# dbusers - manage MySQL user table
use DBI;
use User::pwent;
$dbh = DBI->connect ( 'DBI:mysql:dbname:mysqlserver.domain.com:3306',
'user', 'password',
{ RaiseError => 1 } )
or die "connecting : $DBI::errstr\n";
$dbh->do ( "CREATE TABLE users (uid INT, login CHAR(8))" );
$sql_fmt = "INSERT INTO users VALUES( %d, %s )";
while ( $user = getpwent )
{
$sql = sprintf ( $sql_fmt, $user->uid, $dbh->quote ( $user->name ) );
$dbh->do ( $sql );
}
$sth = $dbh->prepare ( "SELECT * FROM users WHERE uid < 50" );
$sth->execute;
while ( ( @row ) = $sth->fetchrow )
{
print join ( ", ", map {defined $_ ? $_ : "(null)"} @row ), "\n";
}
$sth->finish;
$dbh->do ( "DROP TABLE users" );
$dbh->disconnect;
#-----------------------------