0

我正在尝试使用 php 脚本将一些 csv 和制表符分隔的 txt 提要导入我的数据库。除了这个之外,它们似乎都可以工作。

我有一个包含数据库连接、禁用键、导入每个提要(通过包含语句)和重新启用键的基本 php 脚本,然后再导出最终结果。

导致我出现问题的提要是这个,或者我应该说 sql 语句。

$sql = "

LOAD DATA LOCAL INFILE '../feeds/feed6.txt'
INTO TABLE supplierimport
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\\r\n'
IGNORE 1 LINES
(@col1,@col2,@col3,@col4,@col5,@col6,@col7,@col8,@col9,@col10,@col11,@col12,@col13,@col14,@col15,@col16)
SET
DistributorID = 546, MPN=@col1, Brand=@col6, DistributorSKU=@col1, `Condition`=@col15, ProductName=@col2, Cost= replace(@col4, ',', ''), Stock=@col5, EAN=@col12, ImageURL=@col11, ETA=@col7";

database_queryModify($sql,$result);

print "Imported...";

所以,关于这个有两个问题:

  1. 使用这个说法是否正确;Cost= replace(@col4, ',', ''),我使用这个语句的原因是因为我将每个供应商的成本导入到cost我的数据库中的列中,但是这个特定的供应商使用千位分隔符,即1000.53在提要中显示为1,000.53,因此没有正确导入我的数据库,所以我找到了替换功能,但想验证我在这种情况下是否正确使用它?

  2. 文件中正好有一半的行被导入到我的数据库中。我发现了这个问题;如您所见,mysql 使用设置字段加载数据本地 infile 语法问题我已经放了 \r\n,但它仍然没有全部导入。

任何人都可以帮忙。

谢谢

编辑:仅以逗号分隔的文本文件中的三行为例。这是从文件中粘贴的,因此我没有在每行之间添加额外的行。此外,在第 3 行,您会看到价格为“1,117.00”,以显示千位分隔符的工作方式:

"Item No.","Description","Description 2","Customers Price","Home stock","Brand Name","Expected date for delivery","Item Group No.","Item Group Name","Item Product Link (Web)","Item Picture Link (Web)","EAN/UPC","Weight","UNSPSC code","Product type code","Warranty"

"/5PL0006","Drum Unit","DK-23","126.00","32","Kyocera","03/11/2013","800002","Drums","http://uk.product.com/product.aspx?id=%2f5PL0006","http://s.pic.eu/products/2_PICTURE-TAKEN.JPG","5711045360824","0.30","44103109","","3M"

"/DK24","DK-24 Drum Unit FS-3750","","1,117.00","8","Dell","07/11/2013","800002","Drums","http://uk.product.com/product.aspx?id=%2fDK24","http://s.pic.eu/products/2_PICTURE-TAKEN.JPG","5711045360718","0.20","44103109","","3M"
4

1 回答 1

1

假设你的桌子看起来像

CREATE TABLE supplierimport 
(
distributorid int, 
MPN varchar(32),
description varchar(100),
description2 varchar(100),
customers_price decimal(19,2),
home_stock int,
brand_name varchar(100),
delivery_date date,
item_group_number varchar(32),
item_group_name varchar(100),
product_link varchar(255),
picture_link varchar(255),
barcode varchar(32),
weight decimal(19,4),
UNSPSC varchar(32),
product_type_code varchar(32),
warranty varchar(32)
);

这将允许您成功加载数据

LOAD DATA LOCAL INFILE '/path/to/feed6.txt'
INTO TABLE supplierimport
    FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    LINES TERMINATED BY '\n' -- or '\r\n' in your case
IGNORE 1 LINES
(MPN, @description, @description2, @customers_price, @home_stock, @brand_name, @delivery_date, 
 @item_group_number, @item_group_name, @product_link, @picture_link, @barcode, @weight, @UNSPSC, @product_type_code, @warranty)
SET distributorid = 546, 
    description       = NULLIF(@description, ''),
    description2      = NULLIF(@description2, ''),
    customers_price   = REPLACE(NULLIF(@customers_price), ',', ''), 
    home_stock        = NULLIF(@home_stock, ''), 
    brand_name        = NULLIF(@brand_name, ''), 
    delivery_date     = STR_TO_DATE(NULLIF(@delivery_date, ''), '%d/%m/%Y'), 
    item_group_number = NULLIF(@item_group_number, ''), 
    item_group_name   = NULLIF(@item_group_name, ''), 
    product_link      = NULLIF(@product_link, ''), 
    picture_link      = NULLIF(@picture_link, ''), 
    barcode           = NULLIF(@barcode, ''), 
    weight            = NULLIF(@weight, ''), 
    UNSPSC            = NULLIF(@UNSPSC, ''), 
    product_type_code = NULLIF(@product_type_code, ''), 
    warranty          = NULLIF(@warranty, '');

我机器上的结果

mysql> 从供应商导入中选择 *;
+---------------+----------+---------- ---+-------------+-----------------+------------+ ------------+---------------+--------------------+- ----------------+------------------------------------------------ -----------------+-------------------------------- --------------+---------------+--------+---------- +-------------------+----------+
| 分销商ID | MPN | 描述 | 描述2 | 客户价格 | home_stock | 品牌名称 | 交货日期 | item_group_number | item_group_name | 产品链接 | 图片链接 | 条码 | 重量 | UNSPSC | 产品类型代码 | 保修 |
+---------------+----------+---------- ---+-------------+-----------------+------------+ ------------+---------------+--------------------+- ----------------+------------------------------------------------ -----------------+-------------------------------- --------------+---------------+--------+---------- +-------------------+----------+
| 第546章 /5PL0006 | 鼓组 | DK-23 | 126.00 | 32 | 京瓷 | 2013-11-03 | 800002 | 鼓 | http://uk.product.com/product.aspx?id=%2f5PL0006 | http://s.pic.eu/products/2_PICTURE-TAKEN.JPG | 5711045360824 | 0.3000 | 44103109 | 空 | 3M |
| 第546章 /DK24 | DK-24 鼓单元 FS-3750 | 空 | 1117.00 | 8 | 戴尔 | 2013-11-07 | 800002 | 鼓 | http://uk.product.com/product.aspx?id=%2fDK24 | http://s.pic.eu/products/2_PICTURE-TAKEN.JPG | 5711045360718 | 0.2000 | 44103109 | 空 | 3M |
+---------------+----------+---------- ---+-------------+-----------------+------------+ ------------+---------------+--------------------+- ----------------+------------------------------------------------ -----------------+-------------------------------- --------------+---------------+--------+---------- +-------------------+----------+
2 行(0.01 秒)
于 2013-10-31T21:09:38.260 回答