【问题标题】:Importing nulls and commas into MYSQL将空值和逗号导入 MYSQL
【发布时间】:2012-08-20 19:21:47
【问题描述】:

我正在像这样将文件导入 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 字段包含下一行的所有数据,就好像它没有检测到以 ".

【问题讨论】:

  • 这是 MySQL 本身的一个错误。 See the link
  • 您能否通过\Ns 并用空白替换它们以便插入,,?你能告诉我们一些被截断的行吗?

标签: mysql csv import null


【解决方案1】:

在闲逛之后,我想通了。

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

Write(rowData + Environment.NewLine);

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

Write(rowData + "\n");

这似乎工作得很好。

【讨论】:

    【解决方案2】:

    看来是行分隔符有问题,请检查它们是否在您的文件中为 '\n',或者尝试在 LOAD DATA 命令中使用 '\r\n'。

    另外,我看不出 \N 有什么问题,它按原样工作。

    【讨论】:

      【解决方案3】:

      mysql 无法识别 '\n' 换行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多