The problem not in INET6_ATON
but in your IPV6.csv
file. Unlike IPV4.csv
you have extra spaces after commas and that makes LOAD DATA
read your second field like this
"2001:200:ffff:ffff:ffff:ffff:ffff:ffff"
^^ ^
and that is why INET6_ATON
returns NULL
.
To fix this:
- either remove extra spaces in your csv file
- or change delimiter to
FIELDS TERMINATED BY ', '
Besides you can simplify your query and just use INET6_ATON
instead of conditionally switching between INET_ATON
and INET6_ATON
. The latter works just fine with both IPV4 and IPV6.
That being said your query might look like this (assuming that you don't have extra spaces in csv file)
LOAD DATA LOCAL INFILE '/Users/Invictus/Documents/htdocs/overkill/etcs/IPV6.csv'
INTO TABLE `overkill`.`geoIP`
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(@ipfrom, @ipto, @dummy, @dummy, @abbr, @dummy)
SET IPFrom = INET6_ATON(@ipfrom),
IPTo = INET6_ATON(@ipto),
countries_countryID =
(
SELECT IF(COUNT(*) != 0, `countries`.`countryID`, 999)
FROM `countries`
WHERE `countries`.`countryAbbreviation` = @abbr
LIMIT 1
);
To test things out I've removed extra spaces, combined both of your files, and loaded them into ipv6
table. Here is the result
mysql> select geoIPID, HEX(ipfrom), HEX(ipto) from ipv6; +---------+----------------------------------+----------------------------------+ | geoIPID | HEX(ipfrom) | HEX(ipto) | +---------+----------------------------------+----------------------------------+ | 1 | 20010200000000000000000000000000 | 20010200FFFFFFFFFFFFFFFFFFFFFFFF | | 2 | 20010208000000000000000000000000 | 20010208FFFFFFFFFFFFFFFFFFFFFFFF | | 3 | 20010218000000000000000000000000 | 20010218FFFFFFFFFFFFFFFFFFFFFFFF | | 4 | 01000000 | 010000FF | | 5 | 01000100 | 010003FF | | 6 | 01000400 | 010007FF | +---------+----------------------------------+----------------------------------+ 6 rows in set (0.00 sec) mysql> select geoIPID, INET6_NTOA(ipfrom), INET6_NTOA(ipto) from ipv6; +---------+--------------------+----------------------------------------+ | geoIPID | INET6_NTOA(ipfrom) | INET6_NTOA(ipto) | +---------+--------------------+----------------------------------------+ | 1 | 2001:200:: | 2001:200:ffff:ffff:ffff:ffff:ffff:ffff | | 2 | 2001:208:: | 2001:208:ffff:ffff:ffff:ffff:ffff:ffff | | 3 | 2001:218:: | 2001:218:ffff:ffff:ffff:ffff:ffff:ffff | | 4 | 1.0.0.0 | 1.0.0.255 | | 5 | 1.0.1.0 | 1.0.3.255 | | 6 | 1.0.4.0 | 1.0.7.255 | +---------+--------------------+----------------------------------------+ 6 rows in set (0.00 sec)