Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

column count error followed by too few bind variables

by baperl (Sexton)
on Aug 02, 2011 at 21:36 UTC ( [id://918140] : perlquestion . print w/replies, xml ) Need Help??

baperl has asked for the wisdom of the Perl Monks concerning the following question:

hi, I have looked at previous posts and so included
use DBIx::Simple; use SQL::Abstract;
but to no avail. I have an INSERT statement in my code (table has 44 columns, first being an auto_increment id) that reads as (I've not included all 43 columns for brevity):
my $q = "INSERT INTO history (`Symbol`,`Company_Name`,`Last_Price`,... +...,`Mktcap`) VALUES (?,?,?,STR_TO_DATE(?,\'%m/%d/%Y'),TIME(?),?,?,?, +?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? +)";
When I only had 24 ?s following TIME(?)(making it a total of 29 values), I was getting the following error:
DBD::mysql::st execute failed: Column count doesn't match value count +at row 1 at line 62.
so, I went in and added 14 ?s towards the end to total it to 43 ?s (as in the above code) and now I get the following error:
DBD::mysql::st execute failed: called with 29 bind variables when 43 a +re needed at line 65.
can you please help me fix it.... I'm including the remaining code:
$sth = $dbh->prepare($q) or die $dbh->errstr; #the below includes 'id' which is auto_increment my @fields = ('id','Symbol','Company Name','Last Price',....,'Market-c +ap'); my @symbols = ('AA','C'); useRealtimeQueryFormat(); my @quotes = getquote(@symbols); while ($cc < 22222) { $cc++; foreach my $q (@quotes) { my @h = @{$q}; join("\t",@h),"\n"; my $c = @h; for ( my $i = 0 ; $i < $c ; $i++ ) { my $j = 1+ $i; $h[$i] =~ s/ +//; $h[$i] =~ s/ %//; $h[$i] = ($h[$i] eq 'N/A') ? undef : $h[$i]; } $sth->execute(@h); } }

Replies are listed 'Best First'.
Re: column count error followed by too few bind variables
by runrig (Abbot) on Aug 02, 2011 at 23:25 UTC
    You need to debug your code. E.g., what do you think this is doing:
    Why is this line there:
    my $j = 1+ $i;
    Did you know you can more simply iterate over elements of an array with, e.g.:
    for my $element (@h)
    use print and/or warn to output messages until your satisfied that all of your variables are what you expect them to be. E.g. I don't know what this does because you didn't provide the source for it, and I'm sure I don't want to see it:
    my @quotes = getquote(@symbols);

      Further to runrig's reply: The presence of the statement
      suggests baperl is not running with strictures (use strict;) or warnings (use warnings;) enabled, which might offer many suggestions about other problems in the code:

      >perl -wMstrict -le "my @h = qw(a bc def ghij); join(qq{\t}, @h), qq{\n}; " Useless use of join or string in void context at -e line 1. Useless use of a constant ( ) in void context at -e line 1.

      Update: Removed extraneous statement from example code.

      thanks runrig, I've been staring at this code and testing and trying so many different iterations that I accidentally left the
      my $j=...
      I will fix the join, and these are the methods I am using
      #!/usr/bin/perl -w use strict; use Finance::YahooQuote; use DBI; use DBD::mysql; use DBIx::Simple; use SQL::Abstract;
      I suspect that is what you were refering to when you said the source for my @quotes.
Re: column count error followed by too few bind variables
by dreadpiratepeter (Priest) on Aug 02, 2011 at 22:13 UTC
    You should always include ALL the code when asking questions. In this case the error is in what you haven't shown. The number of placeholders ('?') in the string you are passing to prepare does not match the number of parameters you are passing to execute.

    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      hi Pete, I updated my post with the remaining code... await your response....thx!
Re: column count error followed by too few bind variables
by Neighbour (Friar) on Aug 03, 2011 at 08:55 UTC

    It might have been mentioned here before, but I'll put it as explicitly as possible:

    There were 2 errors, the first error you fixed yourself by making sure that `Symbol`,`Company_Name`,`Last_Price`,...,`Mktcap` had the same amount of items as the amount of ?'s in your VALUES-list.

    The second error means that mysql now expects values when you execute your query ($sth->execute(@h);). Apparently, your @h contains only 29 values, but there are 43 ?'s that need to be given something (a value or 'undef' if you want it to be NULL).

    If you want to fill all the extra columns (that you initially added all those ?'s for) to be NULL, why don't you just do that in your query?