You haven't shown us the whole script (maybe we should be grateful), but let me suggest the following minor additions and changes to the code that you posted -- try this out and see what happens:
#!/usr/bin/perl use strict; use DBI; my $tablename = 'table_name'; # maybe this is really a file name? my $last_col = 20; # pick a number (any number?) -- you must know the +right one my $colnames = join( ',', map { sprintf( "field%d", $_ ) } 1 .. $last_ +col ); my $placehld = join( ',', map { '?' } 1 .. $last_col ); my $sql = "insert into $tablename ($colnames) values ($placehld)"; print STDERR "Ready to do:\n $sql\n"; # see how that looks open( DATAFILE, "wheres_the_data" ) or die "wheres_the_data: $!"; my $dbh = DBI->new( blah blah ); my $insert_data = $dbh->prepare( $sql ); foreach (<DATAFILE>) { chomp; my (@ROW) = split /,/; if ( @ROW != $last_col ) { # what do you do if the number of "fields" in ROW # is greater than or less than the number of columns # in the table? Think about that, and do it here. } else { $insert_data->execute( @ROW ); } }

So there were a lot of problems with the code you posted, like using "print" where you needed to use "sprintf", not doing "chomp" on the input data file, and really misinterpreting what the DBI man page tells you about how to use placeholders.

I'm hoping that if you start with this version, you'll get what you want more easily.


In reply to Re: array as mysql db rows by graff
in thread array as mysql db rows by tcf03

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.