Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
esteemed monks,

a mysql insert does some silent conversion of values it inserts into a row, like inserting a string > 255 chars into a column that only takes 255 chars, will insert the first 255 chars. I'd like to know how to detect it when that happens. I've tried looking in various DBI attributes, and mysql specific info, but couldn't find anything that warns me about when this truncating happens:

#!/usr/bin/env perl use strict; use DBI; my $dbh = DBI->connect('a','b','c',{RaiseError => 1, PrintWarn => 1, P +rintError => 1}); my $val = 'x' x 254 . 'y' x 50; my $result; $result=$dbh->do("insert into requestattribute values ('',150,'test123 +','$val')"); print "result: '$result'\n"; print "mysql_info: ". $dbh->{'mysql_info'} . "\n"; print "mysql_errno: ". $dbh->{'mysql_errno'} . "\n"; __DATA__ result: '1' mysql_info: mysql_errno: 0
I've written wrappers around this in the past (ie. fetch values from database and compare these to what was originally meant to be inserted), but there must be a better way. Another way I could handle this is to compare rows values with the table specification beforehand, but both methods involve me doing work that the database should be able to do much better (since it is doing the conversion).

Am I missing something that allows me to easily get some kind of error/warning when this truncating happens? If not, how do other people handle this?


In reply to DBI mysql truncation best practice? by eXile

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (12)
    As of 2014-09-18 10:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (111 votes), past polls