Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Mysql and Perl Module

by Anonymous Monk
on Jun 14, 2012 at 09:26 UTC ( #976160=note: print w/replies, xml ) Need Help??


in reply to Re^2: Mysql and Perl Module
in thread Mysql and Perl Module

See this example program Re: Open multiple file handles?

You can easily create a module with a function like this

sub IntoDbImportCSV { my( $dbiconn, # dbi connection string $colnames, # column names $csvfile, # path to csv file $csvargs, # Text::CSV->new options hash ) = @_; my $dbh = DBI->connect( $dbiconn, undef, undef, { RaiseError => 1, PrintError => 1, }, ); open my($infh), '<', $csvfile ... my $csv_in = ... $dbh->begin_work; my $sth = $dbh->prepare($sql); while ( my $row = $csv_in->getline( $infh ) ) { $sth->execute( @{ $row } ); } $dbh->commit; $dbh->disconnect; } IntoDbImportCSV( 'dbi:SQLite:dbname=temp.test.sqlite', '/path/to/foo.cvs', ['ro', 'sham', 'bo' ], { quote_char => '"', sep_char => ',', allow_loose_escapes => 1, empty_is_undef => 1, binary => 1, auto_diag => 1, }, );