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

Fellow Monks, I using activestate perl on windows 2000. This is wrecking my head, can anybody tell me what is wrong with the following script:
#!usr/bin/perl use DBI; use strict; my $one="one"; my $two="two"; my $three="three"; my $four="four"; # connect to the database, assigning the result to $dbh my $database="globalid"; my $host="autolabserver"; my $port="3306"; #connect to the database, assigning the result to $dbh my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host;port=$ +port", 'tester','auto55'); # die if we failed to connect die "Can't connect: " . DBI->errstr() unless $dbh; # all is well! print "Success: connected!\n"; my $sth = $dbh->prepare('INSERT INTO headerinfo (GLOBALID,DECIMAL,CONV +ERTED,COMMENT) VALUES(?,?,?,?)') or die "Can't prepare SQL: " . $dbh->errstr() +; $sth->execute($one,$two,$three,$four) or die "Can't execute SQL: " . $sth->errstr(); # finish and disconnect $dbh->disconnect(); print "finished";
It produces output like this:
Success: connected! finished C:\Documents and Settings\tester\Desktop\from diarmuid>perl connect.pl Success: connected! DBD::mysql::st execute failed: You have an error in your SQL syntax. +Check the manual that corresponds to your MySQL server version for the right syn +tax to use near 'DECIMAL,CONVERTED,COMMENT) VALUES('one at connect.pl line 28. Can't execute SQL: You have an error in your SQL syntax. Check the ma +nual that corresponds to your MySQL server version for the right syntax to use n +ear 'DECIM AL,CONVERTED,COMMENT) VALUES('one at connect.pl line 28.
It looks like its connecting to the databse alright, and I've checked that the database name and field names are correct. When I comment out the lines:
#$sth->execute($one,$two,$three,$four) # or die "Can't execute SQL: " . $sth->errstr();
No error message is produced. I think these two lines are causing the problem but I just can't see it.... Any Help is greatly appreciated.
Jonathan

Replies are listed 'Best First'.
Re: perl DBI question
by bassplayer (Monsignor) on Jun 22, 2004 at 13:31 UTC
    I think the problem is that DECIMAL is a keyword in MySQL. Here is a link that explains it better than I can.

    bassplayer

      D'oh!

      you're exactly right, I changed the name of the field and everything worked fine.
      Valuable lesson learned, and thanks for the swift response
      Jonathan
        Single quoting the field names in the field list ala ('DECIMAL','SOANDSO','BLAHABLAH') might work as well.
Re: perl DBI question
by bradcathey (Prior) on Jun 22, 2004 at 14:50 UTC
    Oh, can I relate to this. I spent 3 hours trying to debug a DB script that was using reserved MySQL words. I feel your pain. I think we get lulled into the comforts of Perl where putting a $ in front of a word takes most of the reserved word stuff moot. You might want to add your revelation to this node from yesterday.

    —Brad
    "Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton