2010-11-09 243 views
0

我的剧本是日期格式插入到数据库

#!/usr/bin/perl 

#--------------------------------------------------------------------- 
# Description: Extract Lab data from text file and insert to database 
#--------------------------------------------------------------------- 

# Modules Required 
use DBI; # check drivers 


#print "vs2-001-001-ma-sampleFile\n"; 


my $filename = "vs2-001-001-ma-sampleFile.txt"; 

my $count = 0 ; #initialize variable $count 
my ($paraval, $paraname, $pararange, $paraunit); #initialise variables for parameters 
#chomp($filename=<>); #uncomment it To use keyboard input. and type filename with extension Ex: fileName.txt or fileName.csv 

open (OUT,">>$filename.csv") || die print "No\t $!"; 
close OUT; 

open (IN,"$filename") || die print "Noo Input. $!"; 
my @file=<IN>; 

#join the lines with # dilimits 
my $string = join('#', @file); 

    $string =~s /[\r]//g; # To remove space. 
$string =~s /[\n]//g; 
$string =~s /[\t]//g; # To remove tab 




# pattern under while loop will do the work. it will take date as 13 Oct 2010 in $1 and rest values in $2 
# $string=~/Equine Profile Plus\s+#(.*?\s+)\s+.*?(Sample.*)##/g 

while($string=~/Equine Profile Plus\s+#(.*?\s+)\s+.*?(Sample.*?)##/g){ 
my($date,$line,$Sample_Type,$Patient_ID,$Sample_Id,$Doctor_Id,$Location,$Rotor,$Serial,$para,$QC,$HEM,$LIP,$ICT); 
$count++; 

my $date=$1; 


$line=$2; 
    if ($line=~/Sample Type:(.*?)#/gis){ 
    $Sample_Type=clean($1); 
    }if ($line=~/Patient ID:(.*?)#/gis){ 
    $Patient_ID=clean($1); 
    }if ($line=~/Sample ID:(.*?)#/gis){ 
    $Sample_Id=clean($1); 
    }if ($line=~/Doctor ID:(.*?)#/gis){ 
    $Doctor_Id=clean($1); 
    }if ($line=~/Location:(.*?)#/gis){ 
    $Location=clean($1); 
    }if ($line=~/Rotor Lot Number:(.*?)#/gis){ 
    $Rotor=clean($1); 
    }if ($line=~/Serial Number:(.*?)#/gis){ 
    $Serial=clean($1); 
    }if ($line=~/#(NA+.*?GLOB.*?)#/gis){ 

    $para=$1; 
    $para =~ s/#/;/g; 
    $para =~ s/\s\s/ /g; #remove spaces. 
    $para =~ s/\s\s/ /g; 
    $para =~ s/\s\s/ /g; 
    $para =~ s/\s\s/ /g; 
    $para =~ s/\s\s/ /g; 
    $para =~ s/\s\s/ /g; 
    $para =~ s/ /:/g; 

    if ($line=~/#QC(.*?) #HEM(.*?) LIP(.*?) ICT(.*?) /gis){ 
    $QC=clean($1); 
    $HEM=clean($2); 
    $LIP=clean($3); 
    $ICT=clean($4); 
    } 
    while($para =~ /(.*?):(.*?):(.*?);/g){ 
    $paraname = $1; 
    $paraval = $2; 
    $pararange = $3; 
    #$paraunit = $4;  

    # convert the date into DBMS-FORMAT 
    my %map = ('Jan' => '01' , 'Feb' => '02', 'Mar' => '03', 'Apr' => '04','May' => '05', 
    'Jun' =>'06' ,'Jul'=>'07', 
    'Aug'=>'08','Sep'=>'09','Oct'=>'10','Nov'=>'11','Dec'=>'12' ); 

    if ($date =~ m/(..).(...).(....)/) { 
     my ($d, $m, $y) = ($1, $2, $3); 
    $out = sprintf $y, $map{$m}, '%d-%02d-%d', $d; 

    } 
    else{ 
    die ; 
    } 
    print "Parsing the input ...\n"; 
    open (OUT,">>$filename.csv") || die print "No"; #data from text file written to a CSV file. 
     print OUT "\"$count\",\"$out\",\"$Sample_Type\",\"$Patient_ID\",\"$Sample_Id\",\"$Doctor_Id\",\"$Location\",\"$Rotor\",\"$Serial\",\"$QC\",\"$HEM\",\"$LIP\",\"$ICT\",\"$paraname\",\"$paraval\",\"$pararange\",\n"; 
    } 


    } 




} 
close OUT; 

#Load csv into mysql 
print "\n inserting into database\n"; 
&loaddata('$filename.csv'); # comment it while not loading into the database. 
print "\n Database insert completed \n"; 
sub clean 
{ 
my ($line) = shift (@_); 
$line =~ s/\n//g; 
$line =~ s/\r//g; 
$line =~ s/^\s+//g; 
$line =~ s/\s\s//g; 
$line =~ s/\s+$//g; 
$line =~ s/#//g; 
return ($line); 
} 



#init the mysql DB 
sub init_dbh{ 

$db=""; 
$host=""; 
$user=""; 
$password=""; 

my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host", 
          $user, 
          $password) 
          or die "Can't connect to database: $DBI::errstr\n"; 

     return $dbh; 

} 

#Load data to mysql table 
sub loaddata{ 
     my ($name) = @_; 
     my $DBH = init_dbh(); 
     my $STH_GO = $DBH->prepare(q{ 
       LOAD DATA LOCAL INFILE 'vs2-001-001-ma-sampleFile.txt.csv' INTO TABLE parameter FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; })or die "ERROR: ". $DBI::errstr; 
     $STH_GO->execute(); 

     } 

这给在CSV文件中的日期格式为13-10-2010,但插入数据库时​​给0000-00-00..please帮助我修改脚本。

回答

0

MySQL中的日期被指定为YYYY-MM-DD,而不是DD-MM-YYYY。调整输出以匹配此格式。

+0

如何更改格式...我没有线索..应该改变在MySQL部分? – user496818 2010-11-09 07:02:57

+0

您需要更改脚本生成的格式。改变你的sprintf调用。顺便说一句,我不知道你怎么有呼叫实际工作你描述的方式,因为模板字符串应该作为第一个参数,而不是第三供应。你可能想要像这样:sprintf'%04d-%02d-%02d',$ y,$ map {$ m},$ d – cdhowie 2010-11-09 07:08:20

+0

我试过这样做.... my%map =('Jan'=> '01', '月'=> '02', '月'=> '03', '月'=> '04', '五一'=> '05', \t \t \t “君'=>' 06' , '月'=> '07', \t \t \t '月'=> '08', '月'=> '09', '月'=> '10',” Nov'=>'11','Dec'=>'12'); \t \t \t如果($日期=〜米/(..).(..).(......)/){ \t \t \t 我的($ d,$米, $ y)=($ 1,$ 2,$ 3); \t \t \t $ out = sprintf'%04d-%02d-%02d',$ y,$ map {$ m},$ d; \t \t \t \t } \t \t 其他\t { \t \t \t 模具; \t \t}但我的输出2010-00-13..n未2010-10-13 – user496818 2010-11-09 10:18:30