0

我创建了一个名为StatesandCities的表,其中包含 3 列:CountryCode(它的外键)、StateCity,如下所示:

create table StatesandCities (CountryCode varchar(2), State varchar(20), city varchar(20),foreign key (CountryCode) references ListOfCountries (CountryCode));

我有一个包含所有值的 .CSV 文件:

CountryCode,State,City
IN,Karnataka,Bangalore
IN,Karnataka,Mysore
IN,Karnataka,Tumkur
IN,Maharashtra,Mumbai
IN,Maharashtra,Thane
IN,Maharashtra,Nasik
GB,Yorkshire,County Hill
GB,SouthWales,Murrey

这正是它在 Excel 工作表中的表现:

CountryCode State   City
IN  Karnataka   Bangalore
IN  Karnataka   Mysore
IN  Karnataka   Tumkur
IN  Maharashtra Mumbai
IN  Maharashtra Thane
IN  Maharashtra Nasik
GB  Yorkshire   County Hill
GB  SouthWales  Murrey

我使用此代码在 MySQL 5.6 命令行客户端中输入 CSV 文件:

LOAD DATA LOCAL INFILE 'H:/Statesncities.csv' INTO TABLE StatesandCities FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ignore 1 lines (CountryCode, State, City);

正在输入这些值,但它们不是我需要的正确表格格式。而且,如果数据值很长,它也会被削减。一切都搞混了:我尝试过更改屏幕大小、页面布局,但似乎没有任何效果。

OUTPUT

输出:

4

2 回答 2

1

试试这个:

LOAD DATA LOCAL INFILE 'H:/countries.csv' INTO TABLE ListOfCountries
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES (CountryName, CountryCode);

IGNORE 1 LINES用来跳过 CSV 标题行。我也有FIELDS ENCLOSED BY选择地转身。

You can refer to LOAD DATA INFILE Syntax section of MySQL manual for more details.

于 2013-11-08T06:02:22.163 回答
0

this should do it !!

BULK INSERT mycountry
        FROM 'C:\CSVmydata\country.csv'
        WITH
        (
        FIRSTROW = 2,
        FIELDTERMINATOR = ',',  --CSV field delimiter
        ROWTERMINATOR = '\n',   --Use to shift the control to next row
        ERRORFILE = 'C:\CSVmydata_error\countryErrorRows.csv',
        TABLOCK
        )
于 2013-11-08T05:48:06.353 回答