Web Hosting Info

Search:

featured partner

The IP to Country Database

Handbook

CSV File Format
Last updated by sandeep on Sat, 07/19/2003 - 17:23
The CSV file contains four fields:
  • Begining of IP address range
  • Ending of IP address range
  • Two-character country code based on ISO 3166
  • Three-character country code based on ISO 3166
  • Country name based on ISO 3166

This is a sample of how the CSV file is structured:
"0033996344","0033996351","GB","GBR","UNITED KINGDOM"
"0050331648","0083886079","US","USA","UNITED STATES"
"0094585424","0094585439","SE","SWE","SWEDEN"
...
Note: All IP addresses are represented as IP Numbers which is the numeric representation of the dotted IP address.


You can import this data into any database by creating a table with the following fields:

FIELD DATA TYPE FIELD DESCRIPTION
IP_FROM NUMERICAL (DOUBLE) Beginning of IP address range.
IP_TO NUMERICAL (DOUBLE) Ending of IP address range.
COUNTRY_CODE2 CHAR(2) Two-character country code based on ISO 3166.
COUNTRY_CODE3 CHAR(3) Three-character country code based on ISO 3166.
COUNTRY_NAME VARCHAR(50) Country name based on ISO 3166


You can query the above table, after you have imported the data into it, to find the country of a corresponding IP Number by issuing the following Select statement:
SELECT COUNTRY_NAME FROM <TableName> WHERE IP_FROM <= IP Number and IP_TO >= IP Number
    Comment viewing options:
    Select your preferred way to display the comments and click 'Save settings' to submit your changes.
    Complete Beginner
    Posted by nickshanks on Sat, 08/09/2003 - 13:53.
    As a complete beginner to SQL, I have just learnt the following, how to set up a database to use this, from scratch. PLEASE NOTE that the csv file you download is supplied with DOS line endings. You must convert these to UNIX line endings or modify the 'terminated by' parameter accordingly.

    Assuming you can download and install MySQL or another database without problem, connect as the root user issue these commands:

    sql> CREATE DATABASE databasename;
    sql> USE databasename;
    sql> CREATE TABLE iptocountry (ip_from int(4), ip_to int(4), country_code2 char(2), country_code3 char(3), country_name varchar(50));
    sql> load data infile '/path/to/ip-to-country.csv' into table iptocountry fields terminated by ',' enclosed by '"' lines terminated by '\n';

    You should now see: "Query OK, 53416 rows affected" or another number which should corrispond exactly with the number of lines in your csv file. Verify this is the case. If not, issue the command "sql> delete from iptocountry" to remove the records, then tweak your load data file command and try again.

    To verify you have got good data in your database, try this:

    sql> select * from iptocountry limit 10;

    You should see the first ten fields of the database printed out. verify they are identical to the first lines of your csv file. To finish off, issue this command:

    sql> optimize table iptocountry

    That will optimise your table in case you have deleted lots of records, and generally make sure it is fast and efficient. Warning! Note the american spelling of optimise. MySQL (for one) does not understand the normal spelling.

    I hope this helps others. It took me about three hours to install, learn and get SQL working. Refer to php.net for instructions on querying databases from PHP.
     
    Posted by sandeep on Tue, 08/12/2003 - 20:02.
    Related Post: MySQL. Look it up... its also got information on indexes.
     
    "int"
    Posted by Ebart on Thu, 08/14/2003 - 05:51.
    I think it should be:
    sql> CREATE TABLE iptocountry (ip_from int(4) unsigned, ip_to int(4) unsigned, country_code2 char(2), country_code3 char(3), country_name varchar(50));

    I had to do this as regular INT's only go up to '2,147,483,647', which is too small for this system, while UNSIGNED INT'S go to '4,294,967,295'

    Thanks for the Great Guide (regardless of the "int" problem).
     
    Posted by sandeep on Thu, 08/14/2003 - 14:33.
    mysql> create table iptoc(
        -> ip_from int(10) unsigned zerofill not null,
        -> ip_to int(10) unsigned zerofill not null,
        -> country_code2 char(2) not null,
        -> country_code3 char(3) not null,
        -> country_name varchar(50) not null,
        -> primary key(ip_from, ip_to)
        -> );
    
     
    Converter
    Posted by Darren884 on Mon, 09/27/2004 - 01:34.
    Here guys I made a script to convert the .csv file to insert queries. Here the table name is iptocountry: Of couse the first php tag line is missing so you'll have to add that. $tablerow) { $tablerow = str_replace('"', '', $tablerow); $tablerow = str_replace(" \n", '', $tablerow); list($field[0], $field[1], $field[2], $field[3], $field[4]) = split(",", trim($tablerow), 5); $output = "INSERT INTO `iptocountry` (ip_from, ip_to, country_code2, country_code3, country_name) VALUES('" . $field[0] . "', '" . $field[1] . "', '" . $field[2] . "', '" . $field[3] . "', '" . $field[4] . "');
    "; print $output; } fclose($handle); ?>
     
    Load data infile - Linux Mysql
    Posted by M3xican on Fri, 01/16/2004 - 22:20.
    Actually, if tou use Mysql and Linux, you may have to change

    load data infile '/path/to/ip-to-country.csv' into table iptocountry fields terminated by ',' enclosed by '"' lines terminated by '\n';

    into

    load data infile '/path/to/ip-to-country.csv' into table iptocountry fields terminated by ',' enclosed by '"' lines terminated by '\r\n';

    M3xican
    Importing csv records in ms-access2000 database
    Posted by mystiq on Fri, 10/17/2003 - 17:35.
    I looked around & didn't saw any article that'll guide me about importing records in a database from the csv file. It can be a pain in the back if you are a first timer. I learned it the hard way but thought that I'd post this here so that other first timers don't have any problem.

    Here's it is then
    -------------------------

    First of all, convert the csv file into MS-Excel workbook & save it.
    To convert the CSV file, simply open it in MS-Excel & then select
    File---> SaveAs & save it as Excel Workbook.

    Then make a database in MS-Access.
    Right click in the empty white area where it displays tables. Select Import from the context menu that pops up. In the import file dialog, select the file type to Microsoft Excel & then browse to the location where you have saved the excel workbook. Select the workbook file & click Import.

    The Import Spreadsheet dialog appears. Make sure the checkbox stating 'First Row Contains Column Headings' is unchecked. Click next. It asks where you would like to store data. the new table option is selected by default. Click next.

    In the field options, name the fields by selecting each field or let it remain like that. You can always change them later. Click Next. It asks to set a primary key. Choose 'No Primary Key'. Click Next. Enter the table name you want to create. Click on Finish.

    The data is imported quickly(depends on your processor & memory). The tablename that you entered is displayed in the tables list. Double click to open it. Or you can open it in Design View to change the field names.

    For convenience below are the suggested field names:-

    name 1st field as ip_from & check that its data type is number & is double.
    name 2nd field as ip_to & check that its data type is number & is double.
    name 3rd field as country_code2 & check that its data type is text.
    name 4th field as country_code3 & check that its data type is text.
    name 5th field as country_name & check that its data type is text.


    ______________________________
    Mystiq
    ^-------------^
    A new Face everyday
     
    Please Help!!!
    Posted by reservedfunds on Wed, 08/18/2004 - 16:49.
    I want on my site visitors greeted by calling their country... But I can not find how to install this script from the scratch!!! Please email me at: bgmtnoffers@yahoo.com
    =)
    Posted by zealivity5 on Thu, 04/07/2005 - 05:56.
    anyone know the proper query for this? i've searched all through mySQL.com and everything i've found doesn't return a result.
     
    Don't forget the trailing ','
    Posted by smoothuser on Thu, 12/15/2005 - 15:09.
    The LOAD DATA INFILE statements will work but only if a trailing ',' is added at the end of each line (at least with the copy of the file dated 2005-12-15)

    I did this by loading the CSV file into vim and then issuing a search-and-replace with %s/$/,/

    Also note that the United Kingdom has theISO code of 'GB' in this file. We use 'UK' internally as the code so I simple updated the table accordingly.
     
    SQL query (windows)
    Posted by The_deViL on Mon, 01/02/2006 - 19:58.
    This query works with the csv file.
    load data local infile "C://somedir//somedir//ip-to-country.csv" 
    into table ip2country fields terminated by ',' enclosed by '"' 
    lines terminated by '\r\n';
    
    Notes:
    1. If you are on windows, c:\ becoms c://
    2. Not all users are allowed to run "load data infile", if you get an access denied use "load data local infile"
    3. My table name is ip2country

    This is executed from either PHP or Mysql client.
     
    The mysql client
    Posted by The_deViL on Mon, 01/02/2006 - 20:04.
    For those who are new to the mysql client do this (assuming you already made your table):
    1. Start -> Run -> c:/mysql/bin/mysql.exe -u username -p 
    Replace username with the username
    2. You will get a DOS box stating "Enter password: "
    3. Write your password.
    4. You are now at the mysql promt, "mysql> "
    5. Write (without the "): "use yourdatabasename;" 
    6. It will say: "Database changed" 
    7. Write: "load data local infile "C://somedir//somedir//ip-to-country.csv" 
    into table ip2country fields terminated by ',' enclosed by '"' 
    lines terminated by '\r\n';" 
    8. Your done
    
    Postgresql
    Posted by rawr on Mon, 01/23/2006 - 03:39.
    COPY FROM '/ip-to-country.csv' WITH DELIMITER ',' CSV QUOTE '"'
    Load data infile - Linux and Mysql
    Posted by volkirik on Sat, 04/08/2006 - 08:27.
    --- GET THE FILE : ---
    wget http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip
    
    unzip ip-to-country.zip; mv ip-to-country.csv iptocountry.csv; rm -f ip-to-country.zip
    
    --- GIVE THE PERMISSIONS : ---
    chown mysql.mysql iptocountry.csv; chmod 666 iptocountry.csv
    
    --- CREATE DATABASE AND TABLE : ---
    CREATE DATABASE databasename;
    
    USE databasename;
    
    CREATE TABLE `iptocountry` (
      `IP_FROM` double NOT NULL default '0',
      `IP_TO` double NOT NULL default '0',
      `COUNTRY_CODE2` char(2) NOT NULL default '',
      `COUNTRY_CODE3` char(3) NOT NULL default '',
      `COUNTRY_NAME` varchar(50) NOT NULL default '',
      KEY `CODE` (`IP_FROM`,`IP_TO`,`COUNTRY_CODE2`)
    ) ENGINE=MyISAM;
    
    --- LOAD DATA FILE INTO TABLE : ---
    LOAD DATA INFILE '/path/to/csv/iptocountry.csv' INTO TABLE `iptocountry` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
    
    Now you should see :
    Query OK, 65290 rows affected (0.52 sec)
    Records: 65290  Deleted: 0  Skipped: 0  Warnings: 0
    
    or another number which should corrispond exactly with the number of lines in your csv file. To verify you have got good data in your table, try this:
    SELECT * FROM `iptocountry` LIMIT 10;
    
    --- REMOVE THE CSV FILE : ---
    rm -f iptocountry.csv
    
    NOTE: You need root privileges to this!
     
    Omg
    Posted by jsjko on Fri, 05/19/2006 - 01:46.
    thx a lot!!
    The small battle with SQLite
    Posted by otherbird on Sun, 09/17/2006 - 07:55.
    Anybody else out there using PHP 5? :)

    For CSV -> SQLite transfer you have the choice of inputting 60,000+ entries into the db one by one, or using .import from the commandline. Assuming you're sane and will use .import, you'll need to change the separator. You could fiddle with the .separator command in the SQLite commandline client (get it from sqlite.org and make sure it's the right version!), but having the SQLite separator defined as a comma doesn't work here.

    There are a lot of ways you could clean up the data for SQLite, but having the CSV data use the default SQLite separator seems to make the most sense. This works, anyway:
    php -a
    Interactive mode enabled
    
    <php
    $data = file('ip-to-country.csv');
    foreach ($data as $line) {
        $line = trim($line, '"');
        $line = str_replace("\"\r\n", "\n", $line);
        $new[] = str_replace('","', '|', $line);
    }
    file_put_contents('cleaned.csv', $new);
    ?>
    ^Z    /* note: that's CTRL Z in case you got confused there... */
    
    sqlite3 ip_tables.db
    SQLite version 3.3.7
    Enter ".help" for instructions
    sqlite> create table iptocountry (
       ...> ip_from int(4),
       ...> ip_to int(4),
       ...> country_code2 char(2),
       ...> country_code3 char(3),
       ...> country_name varchar(50));
    sqlite> .import cleaned.csv iptocountry
    
    /* - and test the data - */
    sqlite> select * from iptocountry where country_name = 'VIET NAM';
    985268224|985399295|VN|VNM|VIET NAM
    1063573680|1063573695|VN|VNM|VIET NAM
    .....  30-odd entries later  ....
    3741057024|3741319167|VN|VNM|VIET NAM
    sqlite> select country_name from iptocountry where ip_to = 3741319167;
    VIET NAM
    sqlite> .quit