perl與mysql連接

使用dbi,如果linux中沒有,則需要安裝

#!/bin/perl

# load module
use DBI;
my $db="mydb";
my $host='localhost';
my $user='myuser;
my $password='mypassword';

# connect
my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host", $user, $password, {RaiseError => 1});

$sql="SELECT IP FROM mydb where ip<>'' GROUP  BY IP HAVING Count(IP)>1";
# rows
$sth = $dbh->prepare($sql);


# my $rows = $dbh->do($sql);
$sth->execute();
while(@ip=$sth->fetchrow_array())
#如果使用需要把數組中包含的多字段分隔,使用(($filed1,$field2,……,$filedn)=$sth->fetchrow_array()),會自動把獲取的字段值賦給變量
{
  $sql1="update radcheck set ip='' where ip='".$ip."'";
  print "sql1:".$sql1."/n";
  $dbh->do($sql);            #直接執行無返回值命令使用->do
}
$dbh->disconnect();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章