I've been away from perl for too long, and I'm missing something silly. I have a series of text files that I'm reading and
  • changing each line to pipe-delimited
  • writing to a new file
  • inserting into a database table
    The pipe de-limiting and writing to a separate file are going fine. But I'm missing something critical on the insert. I'm using an example from gmax's DBI Recipe Tutorial. After I read in the record, I pipe-delimit it and then split it into an array. I then attempt to insert the array into the database, where things go badly. Code and error messages are below.
    #!/usr/bin/perl use strict; use DBI; #configurables my $workdir = "./work"; my $roster = ""; my $dbh = DBI->connect("DBI:mysql:database=esms", 'username', 'password', ) or die "Can't connect to database"; my @fields = qw (player_name player_age player_nat player_prs player_s +t player_tk player_ps player_sh player_stamina player_aggression player_kab player_tab player_pab player_sab player_games player_sav player_ktk player_ +kps player_sht player_gls player_dpoints player_inj playe +r_sus player_fit); my $fieldlist = join ", ", @fields; my $field_placeholders = join ", ", map {'?'} @fields; my $insert_query = qq{ INSERT INTO employees ( $fieldlist ) VALUES ( $field_placeholders )}; my $sth= $dbh->prepare( $insert_query ); opendir DIR, $workdir; @files = grep /ja/, readdir DIR; closedir DIR; foreach (@files) { $roster = $_; print"now processing $roster\n"; open (RF,"./work/$roster"); open (UNL,">./work/$roster.unl"); while (<RF>) { chomp; next if m/^Name/; next if m/----/; print "$_\n"; $_ =~ s/\s+/|/g; my @player_rec = split '|'; if ($sth->execute(@player_rec)) { print "Worked, cool"; } else { print "Didn't work, uncool, error is: $DBI::errstr"; } print UNL "$_\n"; } close(RF); close(UNL); }
    Resulting Errors:
    Didn't work, uncool, error is: called with 71 bind variables when 24 a +re neededZ_Otyusyo|24|ire|R|3|5|11|5|55|21|300|300|300|300|0|0|0|0|0| +0|0|0|0|0|100| DBD::mysql::st execute failed: called with 74 bind variables when 24 a +re needed at ./planb.pl line 45, <RF> line 17. Didn't work, uncool, error is: called with 74 bind variables when 24 a +re neededP_Choonduaga|25|cam|R|3|7|10|7|57|35|300|300|300|300|0|0|0|0 +|0|0|0|0|0|0|100| DBD::mysql::st execute failed: called with 77 bind variables when 24 a +re needed at ./planb.pl line 45, <RF> line 18. Didn't work, uncool, error is: called with 77 bind variables when 24 a +re neededV_Itroidro|19|ita|LC|3|5|10|7|42|24|300|300|300|300|0|0|0|0| +0|0|0|0|0|0|100| DBD::mysql::st execute failed: called with 76 bind variables when 24 a +re needed at ./planb.pl line 45, <RF> line 19. Didn't work, uncool, error is: called with 76 bind variables when 24 a +re neededK_Ogoejheb|23|jap|C|3|9|9|8|45|28|300|300|300|300|0|0|0|0|0| +0|0|0|0|0|100|
    and so on.......

    Janitored by holli - added readmore tags

    Edit. Fixed Tutorial Link dthacker


    Dave
    Code On!

    In reply to DBI reports too many bind variables by dthacker

    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.