将空值和逗号导入MYSQL

时间:2012-08-20 19:21:47

标签: mysql csv import null

我正在将文件导入MYSQL,如下所示:

LOAD DATA LOCAL INFILE 'C:\\Users\\Justin.Mathieu\\Documents\\data.csv' into table Ingredients
fields terminated by ','
optionally enclosed by '"' 
lines terminated by '\n'
(iVendor, iName, iOrderNumber, iCaseQty, iUnitSize, iDrainSize, iUnitSoldBy, iCaseCost, iVolumeConversion, iUnitLabel, iIngredients)

以及一些导入数据的示例:

VendorName,"Apple Whole Peeled Cored Grd A Fz",61318,1,\N,\N,\N,38.17,\N,\N,\N
VendorName,"Apples- Applesauce Sweetened",10738,6,\N,\N,-1,24,\N,\N,\N
VendorName,"Applesauce Unswtn Fancy",10676,6,106,\N,-1,23.97,\N,\N,"Apples, Water, Erythorbic Acid (To Maintain Color)"
VendorName,Apron 24"X42" White Reg Wt - Dispenser ,81463,1,\N,\N,\N,9.98,\N,\N,\N
VendorName,"Artichoke Heart 40-50Cnt Quartered,     Marintated",10722,6,88,\N,-1,71.46,\N,,"Artichoke Hearts, Vegetable Oil (Soya Or Sunflower), Water, Vinegar"
VendorName,"Artichoke Heart Crse Ct",77334,6,\N,55,\N,49.16,\N,\N,\N
VendorName,"Artichoke Heart Quarters",11804,6,\N,\N,-1,62.46,\N,\N,"Artichoke Hearts, Water, Salt And Citric Acid"
VendorName,"Artichoke Quarters Mixed Cuts",D6808,6,\N,\N,\N,60.26,\N,\N,\N
VendorName,"Bacon Bit Real Ref",56188,2,\N,\N,\N,51.22,\N,\N,\N
VendorName,"Barley Pearled Dry Us #1 Standard",33050,2,\N,\N,\N,21.38,\N,\N,\N

第一个问题是使用\ N.在导入后查看我的数据库,而不是获取空值,我在iIngredients字段中得到“N”。

第二个问题是大部分行都没有导入,以下是我得到的错误示例:

164 row(s) affected, 64 warning(s): 
1262 Row 3 was truncated; it contained more data than there were input columns 
1262 Row 5 was truncated; it contained more data than there were input columns 
1262 Row 9 was truncated; it contained more data than there were input columns 
1262 Row 10 was truncated; it contained more data than there were input columns 
1262 Row 11 was truncated; it contained more data than there were input columns 
1262 Row 12 was truncated; it contained more data than there were input columns 
Records: 164  Deleted: 0  Skipped: 0  Warnings: 94

我已经验证每行都有可能包含要用“”包装的逗号的数据。

有什么想法可能会发生什么?

编辑:我能够通过对字符串行使用“”而对非字符串行使用\ N来解决\ N问题。

编辑2:在被截断的行中,iIngredients字段包含下一行的所有数据,就好像它没有检测到以“。”结尾的行的换行符一样。 / p>

3 个答案:

答案 0 :(得分:2)

在经过足够的涂鸦之后我就明白了。

问题在于,当我将文件转换为为空字段插入“/ N”时(在C#中),我正在使用:

Write(rowData + Environment.NewLine);

显然MySQL真的不喜欢这样,所以我把它改成了:

Write(rowData + "\n");

这似乎运作得很好。

答案 1 :(得分:0)

行分隔符看起来有问题,检查文件中是否为“\ n”,或者尝试在LOAD DATA命令中使用“\ r \ n”。

另外,我没有看到\ N有任何问题,它按原样运行。

答案 2 :(得分:0)

mysql无法识别' \ n'换行