the line of CSV looks like:
-------------
1051;3;10;1;900;EQPT_TYPE_B;B;2.00;2.00;0.00;1.00;0.00;1
-------------
and lines are separated "\n" (LF visible in Notepad++)
use strict; use warnings; use Time::Piece; use DBD::mysql; use TMT_database; #own module use Text::CSV_XS; my $config = init_params(); my $dbh = mysql_db_open($config->{'mysql_server'},'uni',$config->{'mys +ql_user'},$config->{'mysql_pwd'}); print localtime."\n"; my $csv_dbh = Text::CSV_XS->new ({ sep_char => ';', binary => 1, blank_is_undef => 1, empty_is_undef => 1, } ); ###### removing garbage from the file local $^I = '.dirty'; local @ARGV = ( "inputFile.txt" ); while ( <>){ s/--VER.*\n|--DAT.*\n|--EN.*\n|--EIN.*\n|\/|\\//g; s/null|NULL//g; s/[^\x00-\x7E]//g; #removing non proper ascii s/(?m)(?:^|;)[^;\n]{30}\K[^;\n]+//g; #truncating the field to 30 +chars print; } ################################################################### ###### parsing and inserting into mysql open my $io, "<","inoutFile.txt"; my @headerFields = @{$csv_dbh->getline ($io)}; my $row = {}; $csv_dbh->bind_columns (\@{$row}{@headerFields}); print localtime."\n"; my $fieldList = join ",", @headerFields; my $field_placeholders = join ",", map {'?'} @headerFields; my $query = qq{INSERT INTO some_table (id,$fieldList) VALUES (id,$fiel +d_placeholders)}; my $mysql_sth = $dbh->prepare($query); while ($csv_dbh->getline ($io)) { my @fieldstoInsert = map { $row->{$_}} @headerFields; if ( $fieldstoInsert[0] =~ /^\d{1,}$/ ){ $mysql_sth->execute(@fieldstoInsert); $mysql_sth->finish; } } print localtime."\n"; ####################################################################
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |