Tags / Mysql
DBD::mysql Integer Type Coercion
DBD::mysql
knows that certain MySQL column types are integers with this call:
$sth->execute;
$nums = $sth->{'mysql_is_num'} || [];
You can iterate over $nums
and see which columns are numeric-type columns. However, when you pull the data out, it’s converted to a string. So, what used to be as simple as this:
my $accounts = $sth->fetchall_arrayref({});
now requires this kind of trickery:
my @accounts = ();
my $is_num = $sth->{'mysql_is_num'} || [];
while (my $row = $sth->fetchrow_arrayref) {
push @accounts, {
map {
$Acct_Fields[$_] => ## key
($is_num->[$_] ## check to see if it's int
? 0 + $row->[$_] ## coerce to integer/numeric
: $row->[$_]) ## leave it alone
} (0 .. $#Acct_Fields) ## number of keys
};
}
This seems to work as a primary test to see if a scalar ($val
) is an number-ish thing:
SQL Fast Column Add
I found a fast way to add a new column to a table.
MySQL
-
Create the new table like the old table (
foo
), but with the new column you want in it. Make the new table with a different name (foo_new
): -
Add the records from the old table into the new table:
INSERT INTO foo_new (col1, col2, col3) SELECT col1, col2, col3 FROM foo
-
If you have a lot of records, you can disable index updates during insert: