Обсуждение: DBI && INSERT

Поиск
Список
Период
Сортировка

DBI && INSERT

От
Matthias Apitz
Дата:
Hello,

I spend today some hours to nail down and insert problem into our
database with DBI like:

   my $rc = $my_dbh->do($my_sqlstatement);

which returns 1 in $rc (which the following flow in our script took as an
error). If one fired up the same string in $my_sqlstatement with pgsql
the result was always

   INSERT 0 1

At the end of the day I watched with strace the communication on the
network between the DBI client and the PG server:

...
13576 sendto(3,
"6f7374656e20676573636872696562656e3a303a0a726561645f656469666163745f66696c65733a3a6d61696e20456e646520616d2031372e30322e3230323020756d2031363a30303a3234205568720a6c6f67206265656e6465742c206a65747a74207769726420696e7365727420646573206c6f677320696e206163715f7661726461746120766572737563687420286e757220696d207472616365207369636874626172290a',
\n             1)\0", 359, MSG_NOSIGNAL, NULL, 0) = 359
 
13576 poll([{fd=3, events=POLLIN|POLLERR}], 1, 4294967295) = 1 ([{fd=3, revents=POLLIN}])
13576 recvfrom(3, "C\0\0\0\17INSERT 0 1\0Z\0\0\0\5I", 32768, 0, NULL, NULL) = 22
13576 write(4, "    <- do= ( 1 ) [1 items] at read_edifact_files.pl line 8831\n", 62) = 62
13576 write(4, "    -> $DBI::errstr (&) FETCH from lasth=HASH\n", 46) = 46
13576 write(4, "    <- $DBI::errstr= undef\n", 27) = 27
...

i.e. the response on the network was (always) also 'INSERT 0 1' and DBI
misinterprets this as an error condition with 'undef' $DBI::errstr
string.

    matthias
-- 
Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub



Re: DBI && INSERT

От
Gianni Ceccarelli
Дата:
On Mon, 17 Feb 2020 16:49:27 +0100
Matthias Apitz <guru@unixarea.de> wrote:
> I spend today some hours to nail down and insert problem into our
> database with DBI like:
> 
>    my $rc = $my_dbh->do($my_sqlstatement);
> 
> which returns 1 in $rc (which the following flow in our script took
> as an error).

The DBI docs say::

  $rows = $dbh->do($statement) or die $dbh->errstr;

so when ``do`` returns 1, it means it worked, not that it failed.

In particular:

    Returns the number of rows affected or "undef" on error. A return
    value of "-1" means the number of rows is not known, not
    applicable, or not available.

-- 
    Dakkar - <Mobilis in mobile>
    GPG public key fingerprint = A071 E618 DD2C 5901 9574
                                 6FE2 40EA 9883 7519 3F88
                        key id = 0x75193F88