2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

XML形式のデータをMySQLにロードする

Last updated at Posted at 2019-02-02

Q. mysqldumpで生成したxml形式のダンプデータをリストアしたい。
A. LOAD XML 構文を使えばできる。

mysqlを起動し、ログイン

$ ls  #xmlファイルが存在することを確認
person.xml
$ mysql.server start 
$ mysql -u [ユーザ名] -p 

XMLファイルの中身はこんな感じ(mysqldumpで作成できる)

person.xml
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="practice">
	<table_structure name="person">
		<field Field="id" Type="int(11)" Null="YES" Key="" Extra="" Comment="" />
		<field Field="name" Type="varchar(255)" Null="YES" Key="" Extra="" Comment="" />
		<options Name="person" Engine="InnoDB" Version="10" Row_format="Dynamic" Rows="2" Avg_row_length="8192" Data_length="16384" Max_data_length="0" Index_length="0" Data_free="0" Create_time="2019-02-02 14:49:49" Update_time="2019-02-02 14:55:38" Collation="utf8_general_ci" Create_options="" Comment="" />
	</table_structure>
	<table_data name="person">
	<row>
		<field name="id">1</field>
		<field name="name">usera</field>
	</row>
	<row>
		<field name="id">2</field>
		<field name="name">userb</field>
	</row>
	</table_data>
</database>
</mysqldump>

XMLファイルをMySQLにロードする

personテーブルのデータをリストアする

mysql> USE [データベース名];
mysql> TRUNCATE person; 
mysql> LOAD XML LOCAL INFILE 'person.xml' INTO TABLE person;

結果

mysql> SELECT * FROM person;

+------+-------+
| id   | name  |
+------+-------+
|    1 | usera |
|    2 | userb |
+------+-------+
2 rows in set (0.01 sec)

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?