Creating a Country database table from CSV file

Last post 10-27-2008 1:13 PM by raghav_khunger. 8 replies.

Sort Posts:

  • Creating a Country database table from CSV file

    10-27-2008, 10:18 AM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

    hi,

    i found the link below where you can download a CSV file of countries.

    http://www.floresense.com/resc_center/?art=1360

     Now i dont know how to actually create the table in my database which is sql express 2005.

    I have management studio express and have attached a database named MasterDB. I want the country table to

    go in there. Do i click on tables and insert new query?. where would I get the query for this?

    I am also using visual web developer and coding in c sharp

     

    advice appreicated!

     

     

  • Re: Creating a Country database table from CSV file

    10-27-2008, 11:23 AM
    Answer
    • All-Star
      87,430 point All-Star
    • limno
    • Member since 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 4,970
    • Moderator
      TrustedFriends-MVPs

    Instead of using BULK INSERT for this CSV file, you can import the file into an Access database first and from your management studio, you can import the table to your "MasterDB".

    Limno

    Format your SQL query with instant sql formatter:
    http://www.dpriver.com/pp/sqlformat.htm
  • Re: Creating a Country database table from CSV file

    10-27-2008, 11:47 AM
    Answer
    • All-Star
      19,812 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,593
    • TrustedFriends-MVPs

    Hi,amereto2k

    amereto2k:

    i found the link below where you can download a CSV file of countries.

    http://www.floresense.com/resc_center/?art=1360

    from ur specified article download that csv file open it and then remove first row from that

    where all columns are written .

    Now save that file  in ur C:\  with name countrylist and close it .

    ie ur path shuld be like that c:\countrylist.csv

    Now in sql write

     

    CREATE TABLE country

    (

    [Sort Order] int , [Common Name] varchar(300), [Formal Name]varchar(300),

    [Type] varchar(300), [Sub Type] varchar(300),

    [Sovereignty] varchar(300), [Capital] varchar(300),

    [ISO 4217 Currency Code] varchar(300), [ISO 4217 Currency Name] varchar(300),

    [ITU-T Telephone Code] varchar(300), [ISO 3166-1 2 Letter Code] varchar(300),

    [ISO 3166-1 3 Letter Code] varchar(300), [ISO 3166-1 Number] varchar(300),

    [IANA Country Code TLD] varchar(300)

    )

    GO

    BULK

    INSERT country

    FROM 'c:\countrylist.csv'

    WITH

    (

    FIELDTERMINATOR = ',',ROWTERMINATOR = '\n'

    )

    GO

    SELECT *

    FROM country

    Go

     

     

    U will get the all data in ur country table.

     

     

     

    PLZ MARK AS ANSWER IF IT HELP U.

    THANKS.

     

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Creating a Country database table from CSV file

    10-27-2008, 12:06 PM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

    Hi, I tryed that but got the following

    Msg 4864, Level 16, State 1, Line 2

    Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (Sort Order).

    (268 row(s) affected)

     

    basically, after deleting row and saving file,  I went to my database in management studion clicked on tables and then insert new query.

    I pasted your code in and hit execute? any reason you know it failed.

     

    Also Can I just ask to which is the primary key because I will wont to run queries against this table with inner joins etc.

     

    thanks

    amereto

     

  • Re: Creating a Country database table from CSV file

    10-27-2008, 12:09 PM
    Answer
    • Member
      608 point Member
    • pogo2346
    • Member since 08-26-2008, 1:56 PM
    • Saint Petersburg, FL
    • Posts 143

    In the SQL Server 2005 Developer's edition, you right click on the database name, then click on tasks, then on import.  That will import to a table created based on the select query.  Doesn't Express have that feature?  If not, you might want to think about upgrading to the developer's edition.  Bulk insert will work as well, or you could just roll you own import program in c#, but that seems pretty much unnecessary.

    Mark in St. Pete, FL
    icq:421115712
  • Re: Creating a Country database table from CSV file

    10-27-2008, 12:21 PM
    • All-Star
      19,812 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,593
    • TrustedFriends-MVPs

    Hi,amereto2k

    Have u tried my code from begining that also created table from that

    or u have table exist before if that

    Then Plz Post ur create table script

    U r saying that

    amereto2k:

    Msg 4864, Level 16, State 1, Line 2

    Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (Sort Order).

     

     

    That Code is running correctly at my side .

    I have checked after deleting first row from csv file.

     

     

    So plz post ur table script.

     

    Thanks.

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Creating a Country database table from CSV file

    10-27-2008, 12:23 PM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

    I went to the database and in Tasks there is no import option -  just the below. I may be able to get developer version later but for

    now I just want to get this country table to my existing database so I  begin running test queries against for my pilot project.

    detatch

    shrink

    backup

    Restore

    Generate scripts

     

  • Re: Creating a Country database table from CSV file

    10-27-2008, 1:00 PM
    • Member
      237 point Member
    • amereto2k
    • Member since 01-30-2006, 4:31 PM
    • Posts 386

    hi,

    I deleted  [Sort Order] int , from the query and now it worked.

    Sort order does show in the created table so I guess its ok right?

    rgds

     

  • Re: Creating a Country database table from CSV file

    10-27-2008, 1:13 PM
    Answer
    • All-Star
      19,812 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,593
    • TrustedFriends-MVPs

    Hi,amereto2k

     

    Please Mark The Post As Answer as It Gains U one Point and Help Others To Solve Their Problems

    (It Is at the right top corner of every post with name 'Mark As Answer')

    Thanks.

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


Page 1 of 1 (9 items)