Convert .txt to .sql - Contains a list of country, state, city, .. names

Toni Michel Caubet picture Toni Michel Caubet · Dec 18, 2012 · Viewed 25.1k times · Source

I've been googling a lot for a SQL database with all (or the many as posible) the cities of the world,

But the best I've found is a .txt file with a format such as:

-1049671    Kassel          agglomeration   323216                      
-1049670    Kingston            agglomeration   956716                      
-1049669    Derry           agglomeration   98319                       
-1049668    Birmingham          agglomeration   1000010                     
-1049667    West Palm Beach         agglomeration   1370178                     
-1049666    Springfield         agglomeration   748567                      
-1049665    Philadelphia            agglomeration   6423182                     
-1049664    New York            agglomeration   23637491

And also other lines are such as:

467009670   Lillo           locality    3139    3972    -330    Spain   Kastilien-La 

    Mancha  Toledo  
    467009725   Tresjuncos          locality    363 3972    -275    Spain   Kastilien-La Mancha Cuenca  
    467009743   Alconchel de la Estrella            locality    129 3972    -257    Spain   Kastilien-La Mancha Cuenca  
    467009780   Valverde de Júcar           locality    1284    3972    -220    Spain   Kastilien-La Mancha Cuenca  
    467009793   Piqueras del Castillo           locality    71  3972    -207    Spain   Kastilien-La Mancha Cuenca  
    467009812   Almodóvar del Pinar         locality    452 3972    -188    Spain   Kastilien-La Mancha Cuenca  
    467009822   Paracuellos         locality    123 3972    -178    Spain   Kastilien-La Mancha Cuenca  
    467009857   Mira            locality    780 3972    -143    Spain   Kastilien-La Mancha Cuenca  
    467009965   Estivella           locality    1454    3972    -35 Spain   Valencia    Valence 
    467009975   Faura           locality    3872    3972    -25 Spain   Valencia    Valence 
    467010262   Valldemosa  Valdemosa, Valldemossa      locality    2098    3972    262 Spain   Balearen    Baléares    
    467010287   Lloseta         locality    5939    3972    287 Spain   Balearen    Baléares    
    467010291   Inca            locality    30441   3972    291 Spain   Balearen    Baléares    
    467010865   Terralba            locality    9610    3972    865 Italy   Sardinien

So the thing is I have no idea how to import this to mysql/sql tables.. and then be able to make querys.

Is there a way? Any SQL complete database if not?

Answer

Mariusz Sakowski picture Mariusz Sakowski · Dec 18, 2012

Look at the LOAD DATA LOCAL INFILE statement in mysql

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

basically you will end up with opening your file in excel or something like that, saving as .csv and then issue query

LOAD DATA LOCAL INFILE 'c:\db.csv' INTO TABLE some_table FIELDS TERMINATED BY ';'

of course your 'some_table' has to have appropriate structure and you have to adjust field separator to what you have chosen when saving .csv file.

look at the docs I have provided to see further options, like setting file encoding or ignoring n first lines (useful if your file has header)