Posted on 2006-10-23 15:04:49-07 by julialux
Syntax error when inserting strings into a table
Hi, I experience a problem with the insertion of strings into a table. However, this problem does not occur for all strings and there is no pattern or specific character I could find that triggers the exception. Okay, here is what I do:
my $stmt_resultset = $this->{db_handler}->prepare_cached("insert into resultset(session_id, query_ +id, uri, domain, rank) values(?,?,?,?,?)"); $success = 1; for(my $i = 0; $i < @resultset; $i++) { my $result = $resultset[$i]; $success &&= $stmt_resultset->execute($session_id,$query_id, $this->{db_handler}->quote($result),$this->{db_handler}->quote($param4),$i+1 ); } if($success) { $this->{db_handler}->commit; print STDOUT "..committed" if debug; } else { $this->{db_handler}->rollback; print "Couldn't store resultset: " . $this->{db_handler}->errstr . "\n"; }
Alternatively, I already tried to use bind_param, e.g., as in
# $stmt_resultset->bind_param(1, $session_id, SQL_INTEGER); # $stmt_resultset->bind_param(2, $query_id, SQL_INTEGER); # $stmt_resultset->bind_param(3, $result, SQL_VARCHAR);
and call execute without arguments which however does not work as well.

Here is a sample of the error I get:

DBD::PgPP::st execute failed: ERROR: syntax error at or near "www" at character 133
This is when trying to insert a URL, however, when inspecting the database I find that some urls could have been written, and I cannot make up a pattern or specific character that is the reason - I also increased the size of the column definition without any effect.

Has anyone an idea what the cause of this error might be - how to fix it?

Thanks a lot for your help, Julia
Direct Responses: 3615 | Write a response
Posted on 2006-11-24 10:11:21-08 by jqliu in response to 3308
Re: Syntax error when inserting strings into a table
I found this error in my script. I think it is caused by the DBD::PgPP's method execute. It replaces the ? with the real parameters by using s/\?/xxx/e So when your leading parameter has ? in their value, the following value will be placed with that parameter. it caused syntax error easily. i make a patch to PgPP.pm :
=============================================== @@ -355,11 +355,34 @@ if (@$params != $num_param) { # ... } - my $statement = $sth->{Statement}; +# this code will replace the following parameters in the leading parameters' "?" +# my $statement = $sth->{Statement}; +# for (my $i = 0; $i < $num_param; $i++) { +# my $dbh = $sth->{Database}; +# my $quoted_param = $dbh->quote($params->[$i]); +# $statement =~ s/\?/$quoted_param/e; +# } +# this code will replace the proper parameters. + my $pos = 0; + my $leftStatement = $sth->{Statement}; + my $statement = ""; for (my $i = 0; $i < $num_param; $i++) { my $dbh = $sth->{Database}; my $quoted_param = $dbh->quote($params->[$i]); - $statement =~ s/\?/$quoted_param/e; + $pos = index ($leftStatement, "?", 0); + if ($pos < 0) { + last; + } + if ($pos == 0) { + $statement .= $quoted_param; + } else { + $statement .= substr($leftStatement, 0, $pos) . $quoted_param; + } + if ($pos + 1 == length($leftStatement)) { + last; + } else { + $leftStatement = substr($leftStatement, $pos + 1); + } } my $pgsql = $sth->FETCH('pgpp_handle'); my $result; ===============================================
i tested in my script. it is ok. hope it's helpful to you.
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.