Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others having a coffee break in the Monastery: (7)
    As of 2024-04-24 08:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found