{"id":53,"date":"2004-11-02T02:42:13","date_gmt":"2004-11-02T06:42:13","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/rlucastemp\/2004\/11\/02\/fix-perl-dbi-dbdpg-bind-values-rel"},"modified":"2004-11-02T02:42:13","modified_gmt":"2004-11-02T06:42:13","slug":"fix-perl-dbi-dbdpg-bind-values-rely-on-perls-automatic-numericstrin","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/2004\/11\/02\/fix-perl-dbi-dbdpg-bind-values-rely-on-perls-automatic-numericstrin\/","title":{"rendered":"[FIX] Perl DBI \/ DBD::Pg bind values rely on Perl&#8217;s automatic numeric\/string scalar conversion"},"content":{"rendered":"<p><a name='a55'><\/a><\/p>\n<p>Scenario: you are using DBD::Pg to interface with your database (perhaps directly through DBI, or through an abstraction layer like Class::DBI or DBIx::ContextualFetch) when you get an odd result:<\/p>\n<p>DBD::Pg::st execute failed: ERROR:  parser: parse error at or near [your string, or the part of your string that doesn&#8217;t begin with leading digits] at &#8230;<\/p>\n<p>or<\/p>\n<p>DBD::Pg::db selectrow_array failed: ERROR:  Attribute &#8220;yourstring&#8221; not found at &#8230;<\/p>\n<p>If you look at the PostgreSQL query log, you&#8217;ll see that &#8220;yourstring&#8221; was not properly quoted as a literal in the SQL delivered to the parser.<\/p>\n<p>Since you&#8217;ve either been relying upon your abstraction layer or personally doing the Right Thing and binding your values with the &#8220;WHERE thing=? AND otherthing=?&#8221; syntax, you&#8217;re quite confused &#8212; this should all be quoted.<\/p>\n<p>The problem is that Perl has flagged that scalar as a numeric value, possibly because you used a numeric operator on it (like &gt; or == instead of gt or eq).  The solution is to upgrade to DBD::Pg 1.32 or to explicitly stringify your string as &#8220;$yourstring&#8221;.<\/p>\n<p>Below is the bug filed with CPAN.<br \/>\n&#8212;<br \/>\nMac OS X 10.2, Perl 5.6.0, DBD::Pg 1.22, DBI 1.45<\/p>\n<p>Bind values appear to rely upon Perl&#8217;s automatic numeric\/string scalar conversion in order to determine whether or not to quote.<\/p>\n<p>This bug was discussed on<br \/>\nhttp:\/\/aspn.activestate.com\/ASPN\/Mail\/Message\/perl-DBI-dev\/1607287<\/p>\n<p>my $dbh = DBI-&gt;connect(&#8230;); #connect to Postgres; no errors with SQLite<br \/>\nmy $scalar = &#8220;abc&#8221;;<br \/>\nwarn &#8220;scalar is greater than zero (and now considered numeric)&#8221; if $scalar &gt; 0;<br \/>\nwarn &#8220;dbh-&gt;quote(scalar) works ok: &#8221; . $dbh-&gt;quote($scalar);<br \/>\nwarn &#8220;but bind values do not:&#8221; . $dbh-&gt;selectrow_array(<br \/>\n    &#8220;SELECT 1 WHERE 1=?&#8221;,<br \/>\n    undef,<br \/>\n    ($scalar)<br \/>\n);<\/p>\n<p>Using a numeric operator on the scalar makes Perl auto-convert it to a number; this is interpreted by the magic in Pg.xs as rendering the scalar ineligible for quoting.<\/p>\n<p>One solution is to bind those variables that must be text but might have been numberified with &#8220;$varname&#8221;, thereby stringifying them in the eyes of Perl.<\/p>\n<p><a href='http:\/\/aspn.activestate.com\/ASPN\/Mail\/Message\/perl-DBI-dev\/1607287'>[FIX] Perl DBI \/ DBD::Pg bind values rely on Perl&#8217;s automatic numeric\/string scalar conversion &#8230;<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scenario: you are using DBD::Pg to interface with your database (perhaps directly through DBI, or through an abstraction layer like Class::DBI or DBIx::ContextualFetch) when you get an odd result: DBD::Pg::st execute failed: ERROR: parser: parse error at or near [your string, or the part of your string that doesn&#8217;t begin with leading digits] at &#8230; [&hellip;]<\/p>\n","protected":false},"author":1180,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1460],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-rlucasstories"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/users\/1180"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}