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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.