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

Hey all, Same script... different problem. I'm trying to do some table updating and I get the following error message when I run the script.
DBD::Pg::st execute failed: ERROR: parser: parse error at or near "al +bino" at . /dbinsert line 37, <CSV> line 1. DBD::Pg::st execute failed: ERROR: parser: parse error at or near "al +bino" at . /dbinsert line 37, <CSV> line 1.
Here is my code -
#!/usr/bin/perl -w # # use strict; use DBI qw(:sql_types); # Set global dbase connection info # # my $dsn="dbi:Pg:dbname=tabletest"; my $user="postgres"; my $passwd=""; # create and prepare DBI connection and action # # my ($dbh,$id,$descr,$item,$latin); $dbh = DBI->connect($dsn, $user, $passwd, { RaiseError => 1, AutoCommi +t => 0 }); my $insert00 = $dbh->prepare("INSERT INTO hobby (id,descr,item,latin) +VALUES (?, ?,?,?)")|| die $dbh->errstr; # The next two lines are required for telling the Pg driver what data +type # fields 1 and 3 should be. # $insert00->bind_param(1,$id,SQL_INTEGER)|| die $insert00->errs +tr; $insert00->bind_param(3,$item,SQL_INTEGER)|| die $insert00->er +rstr; # Prepare the update file and begin action loop. # # my $csvfile="/home/bradley/dbtest/item_instk.csv"; open (CSV,"$csvfile"); $id = 1; while (<CSV>) { chomp; ($item,$descr,$latin) = split ('/',$_); $insert00->execute( $id, $descr, $item, $latin )||die $dbh->er +rstr; print ","; ++$id; } # Close and commit dbase changes # # close CSV; #$dbh->commit; $dbh->disconnect;
The "|" delimited file looks like this -
10061|SWORDTAIL ALBINO REG|XIPHOPHORUS HELLERI 10081|SWORDTAIL BLACK REG|XIPHOPHORUS HELLERI 10101|SWORDTAIL BRICK REG|XIPHOPHORUS HELLERI 10121|SWORDTAIL CANDY REG|XIPHOPHORUS HELLERI
I know the problem is with the spaces in the fields. Any suggestions??? Thanks, Bradley
Where ever there is confusion to be had... I'll be there.

Replies are listed 'Best First'.
Re: DBI parse errors
by Cine (Friar) on Aug 22, 2001 at 00:54 UTC
    while (<CSV>) { chomp; ($item,$descr,$latin) = split (/\|/,$_); $insert00->bind_param(1,$id,SQL_INTEGER)|| die $insert00->errs +tr; $insert00->bind_param(2,$descr)|| die $insert00->errstr; $insert00->bind_param(3,$item,SQL_INTEGER)|| die $insert00->er +rstr; $insert00->bind_param(4,$latin)|| die $insert00->errstr; $insert00->execute()||die $dbh->errstr; print ","; ++$id; }


    T I M T O W T D I
Re: DBI parse errors
by dmmiller2k (Chaplain) on Aug 22, 2001 at 16:59 UTC

    I presume it's just a typo in the code you uploaded here, but you are split()-ing a "|"-delimited file on '/'. If this is not a typo, but in fact your actual code, then split() is putting the entire text of each line into your variable $item, leaving $descr and $latin undefined. This would certainly create problems since you are binding $item as an SQL_INTEGER type.

    You also should bind the two other columns. When using prepared statements, I've always bound all parameters. Perhaps I'm unsophisticated, but I cannot grok the advantage of not doing so.

    dmm

    Just call me the Anti-Gates ...
    
Re: DBI parse errors
by dga (Hermit) on Aug 22, 2001 at 00:29 UTC

    If its the spaces then you need to bind those columns to a text type or use $dbh->quote(...) to get the proper quoting characters around the strings.