mysql load xml语句产生0条记录

时间:2016-04-18 18:31:31

标签: mysql xml

我在mySQL工作台查询中使用以下语句:

LOAD XML INFILE 'ProductFile.xml' INTO TABLE myDB.products 
ROWS IDENTIFIED BY '<product>';

生成以下日志:

  

19:15:52 LOAD XML INFILE&#39; ProductFile.xml&#39; INTO TABLE myDB.products ROWS由&#39;&#39; 0行受影响记录:0已删除:0跳过:0警告:0 0.000秒

ProductFile.xml包含以下格式的数据:

<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
  <product>
    <name>Thing 1</name>
    <productUrl>http://www.productsite.com/linkofsomesorttothing1</productUrl>
    <ProductId>1234567890</ProductId>
  </product>
  <product>
    <name>Thing 2</name>
    <productUrl>http://www.productsite.com/linkofsomesorttothing2</productUrl>
    <ProductId>1234567891</ProductId>
  </product>
</products>

myDB.products如下:

CREATE TABLE IF NOT EXISTS `myDB`.`Products` (
  `ProductId` INT UNSIGNED NOT NULL,
  `name` VARCHAR(70) NOT NULL,
  `productUrl` TEXT NULL,
   PRIMARY KEY (`ProductId`),
   UNIQUE INDEX `ProductId_UNIQUE` (`ProductId` ASC)
 ) ENGINE=InnoDB COMMENT='\n';

有谁能告诉我它为什么没有输出?

2 个答案:

答案 0 :(得分:0)

我无法重现这个问题。

档案/path/to/file/ProductFile.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
  <product>
    <name>Thing 1</name>
    <productUrl>http://www.productsite.com/linkofsomesorttothing1</productUrl>
    <ProductId>1234567890</ProductId>
  </product>
  <product>
    <name>Thing 2</name>
    <productUrl>http://www.productsite.com/linkofsomesorttothing2</productUrl>
    <ProductId>1234567891</ProductId>
  </product>
</products>

MySQL命令行

mysql> DROP TABLE IF EXISTS `Products`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `Products` (
    ->   `ProductId` INT UNSIGNED NOT NULL,
    ->   `name` VARCHAR(70) NOT NULL,
    ->   `productUrl` TEXT,
    ->   PRIMARY KEY (`ProductId`),
    ->   UNIQUE KEY `ProductId_UNIQUE` (`ProductId`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='\n';
Query OK, 0 rows affected (0.01 sec)

mysql> LOAD XML INFILE '/path/to/file/ProductFile.xml'
    ->   INTO TABLE `Products`
    ->   ROWS IDENTIFIED BY '<product>';
Query OK, 2 rows affected (0.00 sec)
Records: 2  Deleted: 0  Skipped: 0  Warnings: 0

mysql> SELECT
    ->   `ProductId`,
    ->   `name`,
    ->   `productUrl`
    -> FROM
    ->   `Products`;
+------------+---------+---------------------------------------------------+
| ProductId  | name    | productUrl                                        |
+------------+---------+---------------------------------------------------+
| 1234567890 | Thing 1 | http://www.productsite.com/linkofsomesorttothing1 |
| 1234567891 | Thing 2 | http://www.productsite.com/linkofsomesorttothing2 |
+------------+---------+---------------------------------------------------+
2 rows in set (0.00 sec)

答案 1 :(得分:0)

问题来自字符集。我需要在LOAD语句中指定与ISO-8859-1对应的字符集,即CHARACTER SET latin1。现在它有效!