Perl database connection operations

Source: Internet
Author: 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 $ something = $ mysql_dbh-> prepare ($ SQL );
$ Something-> bind_param (1, $ dir_need );
If ($ COUNT = 2) {$ something-> bind_param (2, "all ");}
Else {$ something-> bind_param (2, $ type [$ count]);}
 
$ Something-> bind_param (3, $ key );
My $ para_index = 4;
For ($ Index = 0; $ index <6; $ index ++)
{
$ Something-> bind_param ($ para_index, $ data_hash-> {$ key}-> {$ name_arr [$ Index]});
$ Para_index ++;
}
$ Something-> execute ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.