using prepared: INSERT into monica.event (msg, source, situation, version, date, hostname, last_uid, sub_origin, component, guid, severity, adapter_host) values (????????????) DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at tmp/test_sql line 41. 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, hostname, last_uid, sub_origin, component, guid, severity, adapter_host) VALUES ('this is a test event, ignore', 'Testing', 'START', '0.1', '2006-11-28 19:13:18', 'test', '', 'SelectDocument', 'IT_SERVICE', '623375574', 'CRITICAL', 'this_box') #### #!/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", localtime), 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 $/;