Ip address Data Type DBI

Ip address Data Type DBI

am 10.05.2006 22:38:40 von lancerset

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,

Re: Ip address Data Type DBI

am 11.05.2006 06:19:27 von Matthew Braid

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

Re: Ip address Data Type DBI

am 11.05.2006 15:44:20 von lancerset

much appreaciated,,,

thanks,