MySQL load data infile into table 的使用

經常使用以下:mysql

Load Data InFile 'C:/Data.txt' Into Table `TableTest` Lines Terminated By '\r\n';
這個語句,字段默認用製表符隔開,每條記錄用換行符隔開,在Windows下換行符爲「\r\n」

sql

C:/Data.txt 文件內容以下面兩行:
1 A
2 B
「1」和「A」之間有一個製表符
這樣就導進兩條記錄了。



自定義語法
Load Data InFile 'C:/Data.txt' Into Table `TableTest` Fields Terminated By ',' Enclosed By '"' Escaped By '"' Lines Terminated By '\r\n';

Fields Terminated By ',' Enclosed By '"' Escaped By '"'
表示每一個字段用逗號分開,內容包含在雙引號內

Lines Terminated By '\r\n';
表示每條數據用換行符分開



和 Load Data InFile 相反的是
Select * From `TableTest` Into OutFile 'C:/Data_OutFile.txt';
把表的數據導出
學習


咱們的示例文本文件:
"我愛你","20","相貌日常,常常耍流氓!哈哈"
"李奎","21","相貌日常,常常耍流氓!哈哈"
"王二米","20","相貌日常,常常耍流氓!哈哈"
"老三","24","很強"
"老四","34","XXXXX"
"老五","52","***%*¥*¥*¥*¥"
"小貓","45","中間省略。。。"
"小狗","12","就會叫"
"小妹","21","PP的很"
"小壞蛋","52","表裏不一"
"上帝他爺","96","很是英俊"
"MM來了","10","。。。"
"歌頌黨","20","社會主義好"
"人民好","20","的確是好"
"老高","10","學習很好"
"斜三","60","眼睛斜了"
"中華之子","100","威武的不行了"
"大米","63","我愛吃"
"蘋果","15","好吃"
咱們的示例表結構:
spa

 CREATE TABLE t0 (orm

id bigint(20) unsigned NOT NULL auto_increment,索引

name char(20) NOT NULL,ip

age tinyint(3) unsigned NOT NULL,rem

description text NOT NULL,it

PRIMARY KEY (id),io

UNIQUE KEY idx_name (name)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 

mysql> load data infile '/tmp/t0.txt' ignore into table t0 character set gbk fields terminated by ',' enclosed by '"' lines terminated by '\n' (name,age,description);


Query OK, 19 rows affected (0.01 sec)
Records: 19 Deleted: 0 Skipped: 0 Warnings: 0

mysql> select * from t0;
+----+----------+-----+----------------------------+
| id | name     | age | description                |
+----+----------+-----+----------------------------+
| 1 | 我愛你   | 20 | 相貌日常,常常耍流氓!哈哈 | 
| 2 | 李奎     | 21 | 相貌日常,常常耍流氓!哈哈 | 
| 3 | 王二米   | 20 | 相貌日常,常常耍流氓!哈哈 | 
| 4 | 老三     | 24 | 很強                       | 
| 5 | 老四     | 34 | XXXXX                      | 
| 6 | 老五     | 52 | ***%*¥*¥*¥*¥           | 
| 7 | 小貓     | 45 | 中間省略。。。             | 
| 8 | 小狗     | 12 | 就會叫                     | 
| 9 | 小妹     | 21 | PP的很                     | 
| 10 | 小壞蛋   | 52 | 表裏不一                   | 
| 11 | 上帝他爺 | 96 | 很是英俊                   | 
| 12 | MM來了   | 10 | 。。。                     | 
| 13 | 歌頌黨   | 20 | 社會主義好                 | 
| 14 | 人民好   | 20 | 的確是好                   | 
| 15 | 老高     | 10 | 學習很好                   | 
| 16 | 斜三     | 60 | 眼睛斜了                   | 
| 17 | 中華之子 | 100 | 威武的不行了               | 
| 18 | 大米     | 63 | 我愛吃                     | 
| 19 | 蘋果     | 15 | 好吃                       | 
+----+----------+-----+----------------------------+
19 rows in set (0.00 sec)

我來講明一下相關的參數
關於個人導入語句,我如今直說兩個,其餘的參考手冊。
character set gbk;

這個字符集必定要寫,要否則就會亂碼或者只導入一部分數據。


ignore into table 


由於name 列加了惟一索引,加這個是爲了不重複數據插入報錯。

加入咱們再次運行這個導入語句就會發現
Query OK, 0 rows affected (0.00 sec)
Records: 19 Deleted: 0 Skipped: 19 Warnings: 0
沒有任何值導入,由於裏面已經有了相同的值。

這裏也能夠用replace into table
MySQL會把相同的先幹掉,再插入新的值。
mysql> load data infile '/tmp/t0.txt' replace into table t0 character set gbk fields terminated by ',' enclosed by '"' lines terminated by '\n' (name,age,description);
Query OK, 38 rows affected (0.00 sec)
Records: 19 Deleted: 19 Skipped: 0 Warnings: 0

mysql> select * from t0;
+----+----------+-----+----------------------------+
| id | name     | age | description                |
+----+----------+-----+----------------------------+
| 20 | 我愛你   | 20 | 相貌日常,常常耍流氓!哈哈 | 
| 21 | 李奎     | 21 | 相貌日常,常常耍流氓!哈哈 | 
| 22 | 王二米   | 20 | 相貌日常,常常耍流氓!哈哈 | 
| 23 | 老三     | 24 | 很強                       | 
| 24 | 老四     | 34 | XXXXX                      | 
| 25 | 老五     | 52 | ***%*¥*¥*¥*¥           | 
| 26 | 小貓     | 45 | 中間省略。。。             | 
| 27 | 小狗     | 12 | 就會叫                     | 
| 28 | 小妹     | 21 | PP的很                     | 
| 29 | 小壞蛋   | 52 | 表裏不一                   | 
| 30 | 上帝他爺 | 96 | 很是英俊                   | 
| 31 | MM來了   | 10 | 。。。                     | 
| 32 | 歌頌黨   | 20 | 社會主義好                 | 
| 33 | 人民好   | 20 | 的確是好                   | 
| 34 | 老高     | 10 | 學習很好                   | 
| 35 | 斜三     | 60 | 眼睛斜了                   | 
| 36 | 中華之子 | 100 | 威武的不行了               | 
| 37 | 大米     | 63 | 我愛吃                     | 
| 38 | 蘋果     | 15 | 好吃                       | 
+----+----------+-----+----------------------------+
19 rows in set (0.00 sec)


(name,age,description);這些也就是具體的表屬性了,指明這個就能夠導入想要的數據。

相關文章
相關標籤/搜索