package edi_mysql; use strict; use warnings; use Text::CSV; use DBI; use vars qw/ $VERSION /; $VERSION = 0.01; sub insert_tab { use Data::Dump; dd\@_; my( $forDbi, $tableCol, $csvfile, $csvargs ) = @_; my( $tablename, @columns ) = @{ $tableCol }; open my($infh), '<', $csvfile or die "Can't open '<', $csvfile : $!"; my $csv_in = my $csv = Text::CSV->new( $csvargs ) or die "Cannot use CSV: ".Text::CSV->error_diag (); my( $dbiconn, $user, $pass ) = @{ $forDbi }; my $dbh = DBI->connect( $dbiconn, $user, $pass, { RaiseError => 1, PrintError => 1, }, ); $dbh->begin_work; my $sth = $dbh->prepare( sprintf "insert into %s (%s) values (%s) on duplicate key update", $dbh->quote_identifier( $tablename ), join( ',', map { $dbh->quote_identifier($_) } @columns ), join( ',', ('?') x @columns ) ); while ( my $row = $csv_in->getline( $infh ) ) { $sth->execute( @{ $row } ); } $dbh->commit; $dbh->disconnect; } 1; ## without return keyword