首页 > 代码库 > perl mysql

perl mysql

$dbh= DBI->connect("dbi:<RDMS>:<database>","<username>","<password>",  or die("Couldn‘t connect");  

 

connect( ) 有若干参数:     数据源。(经常调用的数据源名称,或D S N。)数据源格式由要使用的特定DBD 模块需求来确定。对于MySQL驱动程序,允许的格式如下:     "DBI:mysql:db_name"     "DBI:mysql:db_name:host_name"     对于第一种格式,主机名缺省为localhost,或者127.0.0.1(实际上有其他允许的数据源格式)

 

.数据源 是 应用程序访问数据库所采用的技术,数据源包括ado,dao,oledb和odbc,ado.net,jdbc 等等

⑥fetchrow_array
这个方法取下一行数据并且作为一个字段值数组返回它。范例:
while(@row = $sth->fetchrow_array) {
          print qw($row[0]/t$row[1]/t$row[2]/n);
  }
⑦fetchrow_arrayref
这个方法取下一行数据并且作为一个对一个字段值数组的引用返回它。范例:
while($row_ref = $sth->fetchrow_arrayref) {
          print qw($row_ref->[0]/t$row_ref->[1]/t$row_ref->[2]/n);
  }

$dbh= DBI->connect("dbi:mysql:monitor:127.0.0.1","root","");$dbh->do(SET NAMES gbk;);$dbh2= DBI->connect("dbi:mysql:monitor:127.0.0.1","root","");$dbh2->do(SET NAMES gbk;);$query="select TaskID from t_task where PlanID=$planid and Runed=1 order by TaskID";$sth = $dbh->prepare($query);$sth->execute() or die("first layer select failed:$DBI::errstr");while ($row = $sth->fetchrow_arrayref){    #fetch the result.    $result = "";    $found = 0;    $query2 = "select Result from t_result where TaskID=$$row[0] and RunStatus=0";    $sth2 = $dbh2->prepare($query2);    $sth2->execute() or die("second layer select failed:$DBI::errstr");    while($row2 = $sth2->fetchrow_arrayref)    {        $result = $$row2[0];        $found = 1;    }    $sth2->finish;    if (0==$found)    {        next;    }#clean result.     $query2 = "delete from t_result where TaskID=$$row[0]";     $sth2 = $dbh2->prepare($query2);     $sth2->execute() or die("third layer select 1 failed:$DBI::errstr");     $sth2->finish;     #clean task.     $query2 = "delete from t_task where TaskID=$$row[0]";     $sth2 = $dbh2->prepare($query2);     $sth2->execute() or die("third layer select 2 failed:$DBI::errstr");     $sth2->finish;    if ($result eq "")    {        next;    }    @lines = split(/\n/,$result);    foreach $line (@lines)    {        # print "$date|$hour:$minute|$total|$number_200|$ration\n";        if ($line =~ m"^(.*)\|(\d+):(\d+)\|(\d+)\|(\d+)\|(\d+\.?\d*)$")        {                        $content= "switch_155: ".$1."-".$2.$3." total:".$4."  >200ms:" .$5." ration:".$6."\n";                print $content;                                   }    }}$sth->finish;
$dbh->disconnect();
$dbh2->disconnect();