Ian (Fluxtah) Warwick's blog RSS 2.0
# Wednesday, November 11, 2009

It comes to that point in time when you need a country table in your database and you do not have a list to import at hand, after scouring the web a bit, everything seemed to be out of date, so I wrote a little script to scrape the details from a wikipedia page for the ISO 3166-1 standard for country codes.

I wrote and ran the following quick & dirty JQuery script on that page.

if($('#output_textarea').length == 0){
    $(document.body).append($('<textarea rows="128" id="output_textarea"></textarea>'));
}

document.output_textarea_val = "";

$('#sortable_table_id_0 tr').each(function(idx){

    if(idx > 0){
        document.output_textarea_val += "INSERT INTO country (name, code_alpha_2, code_alpha_3, code_numeric) VALUES(";
        var cells = $('td', this), cellLen = cells.length;
        cells.each(function(cellIdx){
            if(cellIdx < 4){
            document.output_textarea_val += "\"" + $(this).text().trim() + "\"";
            if(cellIdx < cellLen - 2){
            document.output_textarea_val += ", ";
            }
            }
        });
        document.output_textarea_val += ');\n';
    }
});

$('#output_textarea').val(document.output_textarea_val);

This script creates insert statements for mySQL and inserts the results into a text box at the foot of the page, in order for it to work you need to inject jQuery into the document, you can do this with the jQueryify Bookmarklet.

I have tested this script and it qualifies for the "works on my machine" certification programme.

The script produces the following output.

Wednesday, November 11, 2009 1:07:31 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Javascript | SQL
Archive
<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Ian Warwick
Sign In
Statistics
Total Posts: 31
This Year: 2
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2010, Ian Warwick
DasBlog theme 'Business' created by Christoph De Baene (delarou)