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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.