• perl dbi unload utf8脚本


    #!/usr/bin/perl 
    use DBI;
    $db_name='zjzc';
    $ip='127.0.0.1';
    $user="root";
    $passwd="1234567";
    $dbh="";
    $dbh = DBI->connect("dbi:mysql:database=$db_name;host=$ip;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
    $dbh->do("SET NAMES 'utf8'");
    $UNLOAD_SRC_DBCONN = DBI->connect("dbi:mysql:database=$db_name;host=$ip;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
    $UNLOAD_SRC_DBCONN->do("SET NAMES 'utf8'");
       if ( $#ARGV < 0 ){
            print "please input your table name !
    ";
                    exit(-1);
                        }
    my $table_name= "$ARGV[0]"; 
    my $str= "$ARGV[1]"; 
    my $hostSql = qq{SELECT column_name  from information_schema.columns where table_schema='$db_name' and  table_name='$table_name'};
    my $DW_DATA_DT ="";
    my $datafile="A-$table_name.TXT";
    use HTTP::Date qw(time2iso str2time time2iso time2isoz);
    
    my @lstRlst1;
    my @lstRlst;
    my ($COLUMN_NAME);
    $dbh->do("SET NAMES 'utf8'");
    my $selStmt = $dbh->prepare($hostSql);
    $selStmt->execute();
    $selStmt->bind_col(1, $COLUMN_NAME);;
    $selStmt->execute();
    while( $selStmt->fetch() ){
      print "$COLUMN_NAME
    ";
    push  (@lstRlst1 ,$COLUMN_NAME);
      }
      $selStmt->finish;
      $dbh->disconnect;
    my @lstRlst =  (@lstRlst1);
    ##########################################
    #=================全局变量区==========================#
    if ($#ARGV <0){
            print "请输入一个表名参数";
            exit(-1);
        }
    sub printlog
    {
      my ($LogInfo)= @_;
      my $CurrTime = time2iso(time());                   # 当前时间
      if(!defined($LogInfo) ){$LogInfo="";}
      my $StrLog="【${CurrTime}】 	 ${LogInfo} 
    "; 
      
      print $StrLog;
      #print LOGFILE $StrLog;
      }
      
    my $exportOracleSql="SELECT ";  #数据导出的sql
    for (my $m=0;$m<@lstRlst + 0 ;$m++){
     if  ($m != @lstRlst + 0 - 1){
      $exportOracleSql = "$exportOracleSql trim($lstRlst[$m])".", "
    }
    else{
    $exportOracleSql = "$exportOracleSql trim($lstRlst[$m])"}
    print "$exportOracleSql
    ";
    }
    my $exportOracleSql="$exportOracleSql from $db_name.$table_name ";
    print "$exportOracleSql
    ";
    
    sub Exportdata{
    	    
    	    printlog "开始导出数据!";
    	    my $exportsql=$exportOracleSql;
    	    if($exportsql eq "error"){
    	    	return -1;
    	    	};
    
    	     $stmt=$UNLOAD_SRC_DBCONN->prepare($exportsql);
    	    unless ($stmt){
    			printlog "
    执行prepare SQL语句出错:
    ";
    			printlog $DBI::errstr;
    			return -1;
    		}
    	       $stmt->execute;
    		if ($UNLOAD_SRC_DBCONN->err) {
    			printlog "
    执行SQL语句出错:
    "; 
    			printlog $DBI::errstr;
    			
    			return -1;
    		}
    	     my $row=0;
    	     my $size=0;
    	     my $curtime;
    
    	     
    	     
    	     my $writeflagsql;
    	     my $tmpstr="";
    	     $row=0;
    	     my $m=0;              
     open(DATAFILE,">", $datafile) || die (print "Open DATA file failed!!!
    ");
    	     while(my $Rows = $stmt->fetchrow_arrayref){
    	     	$m=0;
    	     	$tmpstr="";
    	     	foreach(@$Rows){
    	     		$tmpstr=$tmpstr.$Rows->[$m]."|";
    	     		$m++;
    	     	}
    	     	print DATAFILE $tmpstr.$DW_DATA_DT."
    ";
    	     	$row++;
    	     	if(($row%10000) == 0){
    	     		printlog "已导出数据$row条!";
    	     	}	     	     	
    	    }
         	
            	$stmt->finish;
            #	print FLAGFILE $datafile,"
    ";
            #	print FLAGFILE $row,"
    ";
            	close(DATAFILE);
            #  close(FLAGFILE);
    
            	$curtime=time2iso(time());
              printlog "数据已成功导出!";
              printlog "一共导出数据${row}条";
                    
              return 1;	
    	
    	}
    Exportdata

  • 相关阅读:
    Maven相关
    System.Math 方法
    css简单实现五角星评分、点赞收藏、展示评分(半颗星、1/3颗星)
    微信小程序动态评分展示/五角星展示/半颗星展示/自定义长度展示
    如何基于layui的laytpl实现数据绑定
    layui表格和弹出框的简单示例
    css超简单实现div页面居中【适合做弹出框】
    如何简单方便的用字体代替页面中常见常用的小图标小按钮(上)
    用css3的cursor:zoom-in/zoom-out实现微博看图片放大镜效果
    完整版ajax+百度echarts实现统计图表demo并随着窗口大小改变而自适应
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351272.html
Copyright © 2020-2023  润新知