perl資料庫連接操作

來源:互聯網
上載者:User

#!/usr/bin/perl

use strict;
use warnings;
use DBI;

opendir (DIR,"dirpath/") ||die "can not open it";
my @connect = readdir(DIR);
closedir DIR;

my $dir_need;
foreach my $con (sort @connect){
    if ($con =~ /-[0-9]{2}-[0-9]{2}_/){
        $dir_need = $con;
    }

}

my $database = "DBI:mysql:database";
my $username = "root";
my $password = "password";
my $mysql_dbh = DBI->connect($database,$username,$password )|| die "Database connection not made: $DBI::errstr";

my $count;
my @type = qw[stf utf merged];
for($count =0;$count<3;$count++)
{

open (PYTHON,"/dirpath/$dir_need/$type[$count]/python/index.html") || die "can not open it";
my @connect = <PYTHON>;
close PYTHON;

my $flag = 0;
my $data_hash ;
my $file_name ;
my @name_arr = qw[statements run excluded branches br_exec coverage];
my @key_name;
my $index = 0;
foreach my $eachline (@connect){
    chomp $eachline;

if($eachline =~ /<td class='name left'><.*?>(.*?)<\//)
{
   $flag = 1;
$file_name = $1;
push(@key_name,$1);
}
elsif($eachline =~ /<td>([0-9]+)<\/td>/ && $flag)
{
   $data_hash->{$file_name}->{$name_arr[$index++]} = $1;
}
elsif($eachline =~ /<td class='right'>(.*)%<\/td>/ && $flag)
{
   $data_hash->{$file_name}->{$name_arr[$index]} = $1;
$index = 0;
$flag = 0;
}
}

foreach my $key (@key_name)
{
    my $sql ="insert into python_coverage (builds_time,framework,file,statements,run,excluded,branches,br_exec,coverage)
                   values (?,?,?,?,?,?,?,?,?)";
my $sth = $mysql_dbh->prepare($sql);
     $sth->bind_param(1,$dir_need);
    if($count == 2) {$sth->bind_param(2,"all"); }
    else { $sth->bind_param(2,$type[$count]); }
 
$sth->bind_param(3,$key);
    my $para_index = 4;
    for($index =0;$index <6;$index++)
{
$sth->bind_param($para_index,$data_hash->{$key}->{$name_arr[$index]});
$para_index++;
}
$sth->execute();

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.