I did some test code of my own. This is with SQLite instead of MySQL, but this is so simple that results should be the same.

The code should fail at the prepare statement (see my test code below). If you have a valid $dbh handle, the prepare simply won't work if in your case column "column2" does not exist, in my case column "abc".

I would include an "or die" clause on the connect() like I did below because you are not guaranteed to have the 'RaiseError' in effect unless the connect succeeds. Think about it, if you omitted say the user name field, the connect() is going to fail and things won't work so well interpreting the attribute hash as a password string! In other words, don't rely on 'RaiseError' working until you are sure that the statement that set that attribute itself worked! Your code may be going wrong right at the connect!

I did some tests with and without "use strict" and "use warnings". This made no difference in the error that I got in this simple test. However there are run time aspects to those and the DBI does work with standard $SIG(__WARN__} and $SIG{__DIE__} signal handlers, so I would leave this extra warning stuff on unless there is an unusual performance requirement and even then don't turn it off until after testing is complete.

#!/usr/bin/perl -w use strict; #result the same without strict or warnings use DBI; my $dbfile = "./SomeDB.sqlite"; my %attr = ( RaiseError => 1); #auto die with error printout my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",\%attr) or die "Couldn't connect to database: " . DBI->errstr; my $s2 = $dbh->prepare("INSERT INTO logs ('abc') VALUES ('xyzzy')"); $s2->execute(); __END__ DBD::SQLite::db prepare failed: table logs has no column named abc process exits with error code 2 in my environment...
At the end of the day, a single connect statement and a single prepare statement is enough to demonstrate what happens when an SQL INSERT into a non-existent column (in an existing table, in my case the "logs" table) is attempted.

In reply to Re: DBI not erroring out on Insert by Marshall
in thread DBI not erroring out on Insert by rethaew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.