|
Posted by Matthew Braid on May 11, 2006, 12:19 am
Please log in for more thread options
onlineviewer wrote:
> Hello,
>
> I'm using perl DBI, and I am trying to insert data into a table, but i
> am
> having trouble using the right data type for ip address. It will insert
> the address but it truncates it. 10.0.0.1 will look like 10
> I've tried almost every type , but no luck. Any suggesstions?
>
> Thank you,
>
If you don't care about in-database comparisons, what's wrong with
varchar (ie, a string)?
If you do care about comparisons and you're only dealing with IPv4,
convert the ip to a number (see below - code contains no error
checking!) and store the number (don't forget to convert back when you
retrieve it - I'll leave that as an excercise for the reader :) )
sub ip4_to_num {
my ($ip) = @_;
my ($num, $pow) = (0, 3);
for my $oct (split /\./, $ip) {
$num += $oct * 256 ** $pow--;
}
return $num;
}
MB
|