LOAD DATA INFILEの他にMYSQLIMPORTプログラムを利用する事によって
データをIMPORTする事が出来ます。
mysqlimportクライアントはLOAD DATA INFILESQLステートメントにコマンドラインインターフェース
を提供します。 mysqlimportに対する殆どのオプションはLOAD DATA INFILE構文の節に直接対応
しています

mysqlimport_11

shell> mysqlimport [options] db_name textfile1 [textfile2 …]

コマンドラインで名づけられた各テキストファイルごとに、mysqlimportはファイルネームの
拡張を取り除き、結果をファイルの内容をインポートするテーブルの名前を決定します。
例えば、patient.txt、patient.text、そしてpatientと名づけられたファイルは全てpatientと
名づけられたファイルにインポートされます。

[root@colinux bin]# ./mysqlimport
./mysqlimport Ver 3.7 Distrib 5.1.39, for pc-linux-gnu (i686)
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Loads tables from text files in various formats. The base name of the
text file must be the name of the table that should be used.
If one uses sockets to connect to the MySQL server, the server will open and
read the text file directly. In other cases the client will open the text
file. The SQL command ‘LOAD DATA INFILE’ is used to import the rows.

Usage: ./mysqlimport [OPTIONS] database textfile…
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
The following groups are read: mysqlimport client
The following options may be given as the first argument:
–print-defaults Print the program argument list and exit
–no-defaults Don’t read default options from any options file
–defaults-file=# Only read default options from the given file #
–defaults-extra-file=# Read this file after the global files are read
–character-sets-dir=name
Directory where character sets are.
–default-character-set=name
Set the default character set.
-c, –columns=name Use only these columns to import the data to. Give the
column names in a comma separated list. This is same as
giving columns to LOAD DATA INFILE.
-C, –compress Use compression in server/client protocol.
-#, –debug[=name] Output debug log. Often this is ‘d:t:o,filename’.
–debug-check Check memory and open file usage at exit.
–debug-info Print some debug info at exit.
-d, –delete First delete all rows from table.
–fields-terminated-by=name
Fields in the textfile are terminated by …
–fields-enclosed-by=name
Fields in the importfile are enclosed by …
–fields-optionally-enclosed-by=name
Fields in the i.file are opt. enclosed by …
–fields-escaped-by=name
Fields in the i.file are escaped by …
-f, –force Continue even if we get an sql-error.
-?, –help Displays this help and exits.
-h, –host=name Connect to host.
-i, –ignore If duplicate unique key was found, keep old row.
–ignore-lines=# Ignore first n lines of data infile.
–lines-terminated-by=name
Lines in the i.file are terminated by …
-L, –local Read all files through the client.
-l, –lock-tables Lock all tables for write (this disables threads).
–low-priority Use LOW_PRIORITY when updating the table.
-p, –password[=name]
Password to use when connecting to server. If password is
not given it’s asked from the tty.
-P, –port=# Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
–protocol=name The protocol of connection (tcp,socket,pipe,memory).
-r, –replace If duplicate unique key was found, replace old row.
-s, –silent Be more silent.
-S, –socket=name Socket file to use for connection.
–use-threads=# Load files in parallel. The argument is the number of
threads to use for loading data.
-u, –user=name User for login if not current user.
-v, –verbose Print info about the various stages.
-V, –version Output version information and exit.

Variables (–variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
——————————— —————————–
character-sets-dir (No default value)
default-character-set utf8
columns (No default value)
compress FALSE
debug-check FALSE
debug-info FALSE
delete FALSE
fields-terminated-by (No default value)
fields-enclosed-by (No default value)
fields-optionally-enclosed-by (No default value)
fields-escaped-by (No default value)
force FALSE
host (No default value)
ignore FALSE
ignore-lines 0
lines-terminated-by (No default value)
local FALSE
lock-tables FALSE
low-priority FALSE
port 3306
replace FALSE
silent FALSE
socket /tmp/mysql.sock
use-threads 0
user (No default value)
verbose FALSE
[root@colinux bin]#

[root@colinux bin]# mysql -e 'CREATE TABLE MYSQLIMP(id INT,n varchar(30))' STUDY -u root -p
Enter password:
[root@colinux bin]# ed
a
100 This is MYSQLIMPORT
101 STUDY AT HOME
102 HAVE A FUN
103 I WISH I CAN SLEEP..
.
w MYSQLIMP.txt
82
q
[root@colinux bin]# od MYSQLIMP.txt
0000000 030061 004460 064124 071551 064440 020163 054515 050523
0000020 044514 050115 051117 005124 030061 004461 052123 042125
0000040 020131 052101 044040 046517 005105 030061 004462 040510
0000060 042526 040440 043040 047125 030412 031460 044411 053440
0000100 051511 020110 020111 040503 020116 046123 042505 027120
0000120 005056
0000122
[root@colinux bin]# od -c MYSQLIMP.txt
0000000 1 0 0 \t T h i s i s M Y S Q
0000020 L I M P O R T \n 1 0 1 \t S T U D
0000040 Y A T H O M E \n 1 0 2 \t H A
0000060 V E A F U N \n 1 0 3 \t I W
0000100 I S H I C A N S L E E P .
0000120 . \n
0000122
[root@colinux bin]# mysqlimport --local STUDY MYSQLIMP.txt -u root -p
Enter password:
STUDY.MYSQLIMP: Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
[root@colinux bin]# mysql -e 'select * from MYSQLIMP' STUDY -u root -p
Enter password:
+——+———————-+
| id | n |
+——+———————-+
| 100 | This is MYSQLIMPORT |
| 101 | STUDY AT HOME |
| 102 | HAVE A FUN |
| 103 | I WISH I CAN SLEEP.. |
+——+———————-+
[root@colinux bin]#

ed_command

※ edコマンド 「aから.まで」で、wでファイルに書き込みqでquit
編集しているときは、文字と文字はタブでスペースを空けてます。

参考サイト
7.14. mysqlimport — データインポートプログラム

MYSQLIMPORT

Post Navigation