eXile has asked for the wisdom of the Perl Monks concerning the following question:
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:
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).#!/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
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: DBI mysql truncation best practice?
by Melly (Chaplain) on Mar 24, 2006 at 20:50 UTC | |
Re: DBI mysql truncation best practice?
by perrin (Chancellor) on Mar 24, 2006 at 21:48 UTC | |
Re: DBI mysql truncation best practice?
by CountZero (Bishop) on Mar 24, 2006 at 22:25 UTC | |
Re: DBI mysql truncation best practice?
by nmerriweather (Friar) on Mar 25, 2006 at 00:10 UTC |
Back to
Seekers of Perl Wisdom