Here we go, csvtest.pl
#!/usr/bin/perl -- use strict; use warnings; use edi_mysql; use Carp::Always; Main( @ARGV ); exit( 0 ); sub Main { my @forDbi = ( 'dbi:SQLite:dbname=temp.test.sqlite', 'username', 'password', ); my $csvargsHashref = { quote_char => '"', sep_char => ":", # no more split /\:/ allow_loose_escapes => 1, empty_is_undef => 1, binary => 1, auto_diag => 1, }; edi_mysql::insert_tab( \@forDbi, [ 'tablename', 'tab1', # column name 'tab2', # column name ], '/data14/docs/konvdocs/ebookkto.txt.old', $csvargsHashref, ); }
instead of \@forDbi you can change edi_mysql::insert_tab so it takes a $dbh instead
And here is edi_mysql.pm
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 upda +te", $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
I've tested it with sqlite, it works
In reply to Re^5: Mysql and Perl Module
by Anonymous Monk
in thread Mysql and Perl Module
by endymion
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |