http://www.perlmonks.org?node_id=586545

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

This has been driving me crazy all day, I have found thousands of hits on the web but nothing that has helped. super search came up pretty dry too.

Trying to prepare with placeholders my SQL does not work, it give the error "Column count doesn't match value count", doing it without placeholders runs no problem. I am using the keys and values from the same hash to do both. I have tried using the $dbh->quote method on all my values just incase but that was no help either.

Here is the ouput of my test script, one entry is inserted by the second non-placeholder method

using prepared: INSERT into monica.event (msg, source, situation, version, date, hostn +ame, last_uid, sub_origin, component, guid, severity, adapter_host) v +alues (????????????) DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at tmp/test_sql line 4 +1. can not execute on DBI::st=HASH(0x20262d0c): Column count doesn't match value count at row 1 using direct: INSERT INTO monica.event (msg, source, situation, version, date, hostn +ame, last_uid, sub_origin, component, guid, severity, adapter_host) V +ALUES ('this is a test event, ignore', 'Testing', 'START', '0.1', '20 +06-11-28 19:13:18', 'test', '', 'SelectDocument', 'IT_SERVICE', '6233 +75574', 'CRITICAL', 'this_box')
Here is a small (well as small as I could get it :) sample showing my trouble
#!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); use DBI; use DBD::mysql; my $user = '*****'; my $pass = '*****'; my $dsn = 'dbi:mysql:xxxx:xxx:xxxx'; my $table = 'monica.event'; my %slots = ( guid => (int rand 1000000000), component => 'IT_SERVICE', source => 'Testing', sub_origin => 'SelectDocument', situation => 'START', hostname => 'test', adapter_host => 'this_box', date => (strftime "%Y-%m-%d %H:%M:%S", loca +ltime), severity => 'CRITICAL', msg => 'this is a test event, ignore', version => '0.1', last_uid => '' ); # now connect and get a database handle my $dbh = DBI->connect( $dsn, $user, $pass) or die "Can't connect DB: $DBI::errstr\n"; # prepare the placeholder method my $slots = join ', ', keys %slots; my $places = join ', ', ('?' x scalar keys %slots); my $p_sql = "INSERT into $table ($slots) values ($places)"; print "using prepared:\n$p_sql\n\n"; # prepare with place holders; my $psth = $dbh->prepare($p_sql) or die "Can't create statement $DBI::errstr\n"; # and then use them $psth->execute(values %slots) or print "can not execute on $psth: $DBI::errstr\n"; print $/; $slots{guid}++; # is unique key # do the less effifcient way my $joinvals = "'". (join "', '", values %slots) . "'"; my $d_sql = "INSERT INTO $table ($slots) VALUES ($joinvals)"; print "using direct:\n$d_sql\n\n"; my $dsth = $dbh->prepare($d_sql) or die "can prepare sql: $DBI::errstr\n"; $dsth->execute() or die "can not execute sql: $DBI::errstr\n"; print STDERR $/;
What on earth am I doing wrong ?

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!