develooper Front page | perl.dbi.users | Postings from August 2009

Re: Retrieving warnings after executing a MySQL INSERT statement

Thread Previous | Thread Next
Peter J. Holzer
August 3, 2009 00:46
Re: Retrieving warnings after executing a MySQL INSERT statement
Message ID:
On 2009-07-28 09:19:05 -0700, David Goodman wrote:
> I suggest that you check to see if the string $DBI::errstr is empty
> rather than just whether the 'do' function executed correctly. 
> It seems that the SQL is correctly submitted from the DBI side but the
> database server actually produces an error message.

The server does not produce an error message. It only produces a
warning. You can see the same behaviour in the mysql command line

    mysql> insert into foo(name, age) values('foo2', 'bar2');
    Query OK, 1 row affected, 1 warning (0.00 sec)

The query is ok, there is no error, but 1 warning.

    mysql> show warnings;
    | Level   | Code | Message                                                   |
    | Warning | 1366 | Incorrect integer value: 'bar2' for column 'age' at row 1 | 
    1 row in set (0.00 sec)

This is MySQL specific, so you have to check the documentation of
DBD::mysql. And sure enough, the first occurrence of "warning" is:

           The number of warnings generated during execution of the SQL

However, the DBI does know about warnings and information, too:

       A driver may return 0 from err() to indicate a warning condition after
       a method call. Similarly, a driver may return an empty string to
       indicate a ’success with information’ condition. In both these cases
       the value is false but not undef. The errstr() and state() methods may
       be used to retrieve extra information in these cases.


   _  | Peter J. Holzer    | Auf jedem Computer sollte der Satz Ludwigs II
|_|_) | Sysadmin WSR       | eingeprägt stehen: "Ein ewig Rätsel will ich
| |   |      | bleiben, mir und andern."
__/   | |    -- Wolfram Heinrich in desd

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About