我有这个XML:
<product>
<id>3</id>
<name>myname</name>
<weight>67</weight>
<price>$34.00</price>
</product>
我也有一个mysql表:
+-----------+--------+-------------+
| ID | Title | Price |
+-----------+--------+-------------+
| 1 | title1 | $19.00 |
| 2 | title2 | $24.00 |
+-----------+--------+-------------+
我尝试使用以下代码将xml文件插入表中:
LOAD XML LOCAL INFILE 'pathtothefile/filename.xml'
INTO TABLE mytable
ROWS IDENTIFIED BY '<product>'
SET ID = id, Title=name, Price=price;
但我收到此错误#1054 - 未知列&#39;名称&#39;在&#39;字段列表&#39;
答案 0 :(得分:0)
试试这个:
LOAD XML LOCAL INFILE 'pathtothefile/filename.xml'
INTO TABLE mytable
ROWS IDENTIFIED BY '<product>'
SET ID = id, Title=`name`, Price=price;
;
或者这个:
LOAD XML LOCAL INFILE 'pathtothefile/filename.xml'
INTO TABLE mytable
ROWS IDENTIFIED BY '<product>'
(ID, Title, Price)
;
答案 1 :(得分:0)
如果相应字段的名称不相同,我会避免尝试编写一个将执行XML to database字段映射的SQL语句。
创建一个临时表,其列名与XML标记相同,或创建类似mytable和ALTER的临时表。那么这些陈述就足够了:
CREATE TEMP TABLE myXMLtable LIKE mytable;
ALTER myXMLtable... (rename the fields like you have them in the XML)
LOAD XML LOCAL INFILE '/whatever/filename.xml'
INTO TABLE myXMLtable
ROWS IDENTIFIED BY '<product>';
然后再次重命名并将myXMLtable条目复制到mytable。