处理mysql load data infile工具中的反斜杠和所有控制字符:
第 1 步,创建您的表:
mysql> create table penguin (id int primary key, chucknorris VARCHAR(4000));
Query OK, 0 rows affected (0.01 sec)
第 2 步,创建要导入的文件并将这些数据放入其中。
1 spacealiens are on route
2 scramble the nimitz\
3 \its species 8472
4 \\\\\\\\\\\\\\\\\\
5 Bonus characters:!@#$%^&*()_+=-[]\|}{;'":/.?>,< anything but tab
第 3 步,插入表格:
mysql> load data local infile '/home/el/foo/textfile.txt' into table penguin
fields terminated by '\t' lines terminated by '\n'
(@col1, @col2) set id=@col1, chucknorris=@col2;
Query OK, 4 rows affected, 1 warning (0.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 1
第 4 步,当然,它会导致这个奇怪的输出:
mysql> select * from penguin;
+----+-----------------------------------------------------------------+
| id | chucknorris |
+----+-----------------------------------------------------------------+
| 1 | spacealiens are on route |
| 2 | scramble the nimitz |
| 3 | |
| 4 | \\\\\\\\\ |
| 5 | Bonus characters:!@#$%^&*()_+=-[]|}{;'":/.?>,< anything but tab |
+----+-----------------------------------------------------------------+
第五步,分析警告:
mysql> show warnings;
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------- ------------------+
| Warning | 1262 | Row 2 was truncated; it contained more data than there |
| | | were input columns |
+---------+------+--------------------------------------------------------+
1 row in set (0.00 sec)
第 6 步,想一想到底出了什么问题:
左侧的反斜杠nimitz
导致 mysql 加载数据解析器将第 2 行的结尾与第 3 行的开头连接起来。然后它撞到一个制表符并将 'scramble the nimitz\n3 放入第 2 行。
第 3 行的其余部分被跳过,因为多余的单词its species 8472
不适合任何地方,它会产生您在上面看到的警告。
第 4 行有 18 个反斜杠,所以没有问题,并且显示为 9 个反斜杠,因为每个反斜杠都被转义了。如果有一个奇数,第 2 行的错误就会发生在第 4 行。
第 5 行的奖励字符正常通过。除了制表符之外,一切都是允许的。
第7步,重置表企鹅:
mysql> delete from penguin;
fields escaped by
第 8 步,使用以下子句加载到您的表中:
mysql> load data local infile '/home/el/foo/textfile.txt' into table penguin
fields terminated by '\t' escaped by '\b'
lines terminated by '\n' (@col1, @col2) set id=@col1,
chucknorris=@col2;
Query OK, 5 rows affected (0.00 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 0
第 9 步,从您的表格中选择,解释结果:
mysql> select * from penguin;
+----+------------------------------------------------------------------+
| id | chucknorris |
+----+------------------------------------------------------------------+
| 1 | spacealiens are on route |
| 2 | scramble the nimitz\ |
| 3 | \its species 8472 |
| 4 | \\\\\\\\\\\\\\\\\\ |
| 5 | Bonus characters:!@#$%^&*()_+=-[]\|}{;'":/.?>,< anything but tab |
+----+------------------------------------------------------------------+
5 rows in set (0.00 sec)
现在一切都如我们所料。第 2 行末尾的反斜杠不会转义换行符。第 3 行之前的反斜杠i
没有任何作用。第 4 行的 18 个反斜杠没有被转义。并且奖金字符通过确定。