mysql--批量插入数据

来源:互联网 发布:淘宝手机店铺免费模板 编辑:程序博客网 时间:2024/05/22 15:06
#!/usr/bin/perl -wuse strict;#Aothor@Laomeng#E-mail:18682093512@163.com=syn1 背景:批量插入指定的格式的数据 nsert_logfile程序的功能:是数据库中的mamc_api库的mamc_report表中插入数据 =cutuse DBI;use Encode;use File::Find;use Data::Dumper;use POSIX qw(strftime);my $bakdate =  strftime("%Y-%m-%d", localtime(time));#today#日志路径存储my $log_file='/root/auto_report/log_2015/';my @filepaths;sub wanted{   push @filepaths,"$File::Find::name" if -f $File::Find::name;}find(\&wanted,$log_file);#数据库账户信息my %mysql = (   'dbname' => "mamc_api",   'host'   => "127.0.0.1",   'port'   => 3306,   'user'   => 'root',   'pass'   => 'abc@198920',);#连接数据库判断是否连接成功my $database ="DBI:mysql:$mysql{'dbname'}";my $dbh = DBI->connect($database,$mysql{'user'},$mysql{'pass'})      or die("Could not make connection to database:$DBI::errstr");   $dbh->do("SET character_set_client='utf8'");   $dbh->do("SET character_set_connection='utf8'");   $dbh->do("SET character_set_results='utf8'");#读取日志文件内容并内容插入数据库中open ERRLOG,">>/root/auto_report/log/openerr.log"; #判断文件是否为空,如果为空产生一条记录chomp (my $date=`date`);open(CHF,">>/root/auto_report/log/checkpro.log"); #程序终止日志unless(@filepaths)   {      print CHF "$date => Array empty,The specified directory is empty:/root/auto_report/log_2015\n";      exit 0;   }my $bakdir="/root/auto_report/logbak$bakdate";if (! -e "$bakdir"){   mkdir "$bakdir",0755 or warn "Can't make '$bakdir':$!\n";}foreach my $value (@filepaths){   if( -z $value)   {     print ERRLOG "$date =>  empty file $value\n";     next;   }   print "$value\n";   my $into_t = $dbh->prepare("INSERT INTO mamc_report(area_name,sys_name,dev_name,ip_addre,wri_date,call_name,api_name,call_count,call_time) VALUES(?,?,?,?,?,?,?,?,?)");   open(FL,'<:encoding(UTF-8)',$value);   while(<FL>)   {      my @v=split (/,/);      $into_t->execute("$v[0]","$v[1]","$v[2]","$v[3]","$v[4]","$v[5]","$v[6]",$v[7],"$v[8]") or die "无法执行SQL语句:$dbh->errstr";   }   open(SUCF,">>/root/auto_report/log/opensucess.log");   print SUCF "$date =>  successful write file $value\n";   `mv $value $bakdir/`; #归档操作的日志文件}close(CHF);close(ERRLOG);close(FL);close(SUCF);
0 0
原创粉丝点击