Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import XML file into MySQL database table using XML_LOAD(); function

Tags:

import

mysql

xml

I have an XML file which looks like this :

    <?xml version="1.0" encoding="UTF-8"?>  <resultset statement="YOUR SQL STATEMENTS TO GENERATE THIS XML FILE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">   <row>     <field name="personal_number">539</field>     <field name="firstname">Name</field>     <field name="lastname">Surname</field>     <field name="email">email.domain.com</field>     <field name="start_time">2011-04-02 13:30:00</field>     <field name="end_time">2011-04-02 18:15:00</field>     <field name="employee_category">1,2,4,5,22,37,38,39,41,43,44</field>   </row>   <row>     <field name="personal_number">539</field>     <field name="firstname">Name</field>     <field name="lastname">Surname</field>     <field name="email">email.domain.com</field>     <field name="start_time">2011-04-02 13:30:00</field>     <field name="end_time">2011-04-02 18:15:00</field>     <field name="employee_category">1,2,4,5,22,37,38,39,41,43,44</field>   </row>   <row>     <field name="personal_number">539</field>     <field name="firstname">Name</field>     <field name="lastname">Surname</field>     <field name="email">email.domain.com</field>     <field name="start_time">2011-04-02 13:30:00</field>     <field name="end_time">2011-04-02 18:15:00</field>     <field name="employee_category">1,2,4,5,22,37,38,39,41,43,44</field>   </row> 

I am trying to import it in MySQL using SQL statement :

use databasename; LOAD XML LOCAL INFILE '/pathtofile/file.xml' INTO TABLE my_tablename; 

The table my_tablename has the following fields :

id (auto increment id) personal_number(varchar) firstname(varchar)  lastname(varchar) email(varchar)  start_time(varchar) end_time(varchar) employee_category(varchar) 

I get error : Error Code: 1136 Column count doesn't match value count at row 1

I am using MySQL 5.1.56

I assume this error occurs because the database table has field id, which is not present in the XML file. How is it possible to import this XML file using MySQL queries of built in functions such that it skips id column during the import and relies on the auto increment function for the id column? Is there some smarter way of handling XML file imports im MySQL? Maybe there is better statement which allows to specify column mapping?

Thank you!

like image 889
Cninroh Avatar asked Mar 30 '11 19:03

Cninroh


People also ask

Does MySQL support XML?

The most common way to store XML in MySQL is to use the LOAD_FILE() function to open an entire XML document, store it in a variable, and insert the variable into a table column.

Which version of MySQL has the load XML option?

MySQL :: MySQL 8.0 Reference Manual :: 13.2. 8 LOAD XML Statement.

How do I import XML into MySQL?

To import data from a XML file into a MySQL table, select the table in Object Browser and select Table -> Import -> Import XML Data Using Load Local... or(Ctrl+Shift+X). Tables: The list of all tables of the currently active database is shown. Select the Table from the list box.


2 Answers

you can specify fields like this:

LOAD XML LOCAL INFILE '/pathtofile/file.xml'  INTO TABLE my_tablename(personal_number, firstname, ...);  
like image 53
dnagirl Avatar answered Sep 20 '22 05:09

dnagirl


Since ID is auto increment, you can also specify ID=NULL as,

LOAD XML LOCAL INFILE '/pathtofile/file.xml' INTO TABLE my_tablename SET ID=NULL;

like image 26
Jayanth Avatar answered Sep 18 '22 05:09

Jayanth