Perl parser cvs na sql
priklad subor cvs obsahuje: data.cvs
ID|MENO|VYSKA|VAHA
1;janko;180;75
2;ferko;195;90
a potrebujem to dostat do tvaru
insert into zoznam values('1','janko','180','75' );
insert into zoznam values('2','ferko','195','90' );
Ale CVS-kovy subor ma asi 8MB.
Velmi pekne dakujem.
Pre pridávanie komentárov sa musíte prihlásiť.
http://search.cpan.org/search?query=csv&mode=all
#!/usr/bin/perl
# PERL MODULE
use Text::CSV::Simple;
# script
my $tplSQL = "INSERT INTO country (short_code, name) VALUES (\"%s\",\"%s\");\n";
my $csvFile="codes.csv";
my $parser = Text::CSV::Simple->new();
my @data = $parser->read_file($csvFile);
foreach(@data) {
printf $tplSQL, @$_[1], @$_[2];
};
malo by stacit teda zmenit jeden riadok:
my $parser = Text::CSV::Simple->new();
zmen na:
my $parser = Text::CSV::Simple->new( sep_char => ';' );
#
use strict;
use warnings;
my $csv_file="/tmp/csvfile.csv";
my $separator=";";
open (CSVFILE, $csv_file);
my @lines = <CSVFILE>;
for my $line(@lines)
{
$line =~ s/\n//;
my @splited_line=split($separator,$line);
print "insert into zoznam values('".$splited_line[0]."','".$splited_line[1]."','".$splited_line[2]."','".$splited_line[3]."');\n";
}