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

I have a subroutine I use to insert a records into a postgres database using DBI and DBD::Pg as a driver. I've used it for years with no problems. $dbh is an established connection, and $data is a reference to a hash with the column names and values to be inserted:
sub insertsql { my ($dbh,$table,$data) = @_; my @qm; my @keys; my @values; my $i = -1; foreach my $k (keys %$data) { if (defined($data->{$k})) { $i++; $keys[$i] = $k; $values[$i] = $data->{$k}; $qm[$i] = '?'; } } my $keylist = join(",",@keys); my $qlist = join(",",@qm); my $sqlstatement = "insert into $table ($keylist) values ($qlist) +returning id"; my $sth = $dbh->prepare($sqlstatement); $sth->execute(@values) || die $sth->errstr; $sth->finish(); return; }
I am using this to insert some crowdtangle data. After the 17th line, $sqlstatement looks like this:

insert into crowdtangle (postUrl,crowdtangle_id,statistics,account,dew +indowfy_content,expandedLinks,score,description,message,posted,media, +subscriberCount,updated,platform,type) values (?,?,?,?,?,?,?,?,?,?,?, +?,?,?,?) returning id

When I execute the query I get this:

DBD::Pg::st execute failed: ERROR: column "posturl" of relation "crow +dtangle" does not exist LINE 1: insert into crowdtangle (postUrl,crowdtangle_id,statistics,a.. +. ^ at c:/perlmodules/SqlSupport.pm lin +e 70.
It is right, column posturl does not exist. The column name is postUrl with a capital U, like I had it in the query. What could be going on here?

Replies are listed 'Best First'.
Re: DBI/DBD::Pg changing my sql query somehow?
by talexb (Chancellor) on Jul 25, 2024 at 03:56 UTC

    Hmm .. I see

      insert into crowdtangle (postUrl, ..
    and then
      DBD::Pg::st execute failed: ERROR: column "posturl" of relation ..
    which leads me to believe that these fields are case-sensitive. (So, the field name should be a uniform choice of posturl, and not postUrl.)

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        Using mixed case for column names is "unwise" if they are not quoted (as mentioned at URLs linked elsewhere in this thread). OP could get on with life with quoting them (in their queries).

        Mostly because someone unwisely created the table with mixed-case column names.

        It's even worse than that. Someone created the table with column names arbitrarily scattered between camelCase and snake_case. That's just inviting doom.


        🦛

Re: DBI/DBD::Pg changing my sql query somehow?
by bliako (Abbot) on Jul 24, 2024 at 19:42 UTC