本文代码运行环境:MySQL:5.1.26-rc-community,Windows 2003

无意中在 emule 的安装目录下看到了个 ip-to-country.csv 文件。 打开后,发现是世界各国及IP段对照的文件。格式如下:

33996344,33996351,GB,GBR,英国50331648,69956103,US,USA,美国69956104,69956111,BM,BMU,百慕达群岛69956112,83886079,US,USA,美国94585424,94585439,SE,SWE,瑞典100663296,121195295,US,USA,美国121195296,121195327,IT,ITA,意大利121195328,152305663,US,USA,美国

真是个好东东!正好一个项目要用到,就准备把数据导入到 MySQL 数据库中。 首先在 MySQL 数据库中建立表结构:

use testdb;

create table ip_to_country(   ip1      int unsigned  not null  ,ip2      int unsigned  not null  ,cname1   varchar(10)   not null  ,cname2   varchar(10)   not null  ,cname3   varchar(50)   not null)  engine=innodb default charset=utf8;

我准备用 MySQL 导入工具:mysqlimport 来完成导入任务。 首先把数据文件 ip-to-country.csv copy 到 d:/,为了使其和 MySQL 中表名匹配, 重命名为 ip_to_country.csv。然后根据数据文件格式,编写并执行下面的 mysqlimport 脚本:

mysqlimport --local            --user=root            --password=123456            --delete            --default-character-set="gb2312"            --fields-terminated-by=","            --fields-enclosed-by=""            --fields-escaped-by=""            --lines-terminated-by="/r/n"            testdb            "d:/ip_to_country.csv"

注意:上面的 mysqlimport 参数应写成一行,为了便于阅读我有意分成多行。 执行上面的 mysqlimport 命令后,发现有 Warnings:

testdb.ip_to_country: Records: 65290  Deleted: 0  Skipped: 0  Warnings: 15

数据已经导入,不管那么多了, 先进 MySQL 数据库看下效果。 首先设置 character_set_results=gb2312; 然后查询 10 条记录出来看看,步骤如下:

mysql> set character_set_results=gb2312;

mysql> show variables like '%char%';+--------------------------+--------------------------+| Variable_name            | Value                    |+--------------------------+--------------------------+| character_set_client     | utf8                     || character_set_connection | utf8                     || character_set_database   | utf8                     || character_set_filesystem | binary                   || character_set_results    | gb2312                   || character_set_server     | utf8                     || character_set_system     | utf8                     || character_sets_dir       | D:/MySQL/share/charsets/ |+--------------------------+--------------------------+
mysql> select * from ip_to_country limit 10;

+-----------+-----------+--------+--------+--------+| ip1       | ip2       | cname1 | cname2 | cname3 |+-----------+-----------+--------+--------+--------+|  33996344 |  33996351 | GB     | GBR    | ?      ||  50331648 |  69956103 | US     | USA    |        ||  69956104 |  69956111 | BM     | BMU    |        ||  69956112 |  83886079 | US     | USA    |        ||  94585424 |  94585439 | SE     | SWE    |        || 100663296 | 121195295 | US     | USA    |        || 121195296 | 121195327 | IT     | ITA    |        || 121195328 | 152305663 | US     | USA    |        || 152305664 | 152338431 | GB     | GBR    | ?      || 152338432 | 167772159 | US     | USA    |        |+-----------+-----------+--------+--------+--------+

结果发现国家的中文名称都是乱码。奇怪,已经把 mysqlimport 的 default-character-set 参数设为:gb2312,为什么会有乱码? 最后不得以,只好在 MySQL 数据库中, 把表 ip_to_country 的字符集改为 gb2312。

mysql> alter table ip_to_country default character set gb2312;mysql> alter table ip_to_country convert to character set gb2312;

然后重新执行导入命令 mysqlimport,这时候发现 MySQL 乱码问题已解决, 中文国家名字可以正常显示:

mysql> select * from ip_to_country limit 10;

+-----------+-----------+--------+--------+------------+| ip1       | ip2       | cname1 | cname2 | cname3     |+-----------+-----------+--------+--------+------------+|  33996344 |  33996351 | GB     | GBR    | 英国         ||  50331648 |  69956103 | US     | USA    | 美国         ||  69956104 |  69956111 | BM     | BMU    | 百慕达群岛   ||  69956112 |  83886079 | US     | USA    | 美国         ||  94585424 |  94585439 | SE     | SWE    | 瑞典         || 100663296 | 121195295 | US     | USA    | 美国         || 121195296 | 121195327 | IT     | ITA    | 意大利       || 121195328 | 152305663 | US     | USA    | 美国         || 152305664 | 152338431 | GB     | GBR    | 英国         || 152338432 | 167772159 | US     | USA    | 美国         |+-----------+-----------+--------+--------+------------+

留下一个问题:mysqlimport 到底能不能把文本文件中的 gb2312 字符 转换成 utf8 导入到 MySQL 数据库中?

虽然问题看起来已经解决了,但我还想试下 MySQL load data 命令。 mysqlimport 虽然把数据导入数据库了,但还有 15 Warnings 在闹心。 我本想利用 mysqlimport 自身的功能来查看这些 Warnings 到底是怎么回事, 但翻翻手册,仍无计可施。MySQL 中有个 show warnings 给我一线希望。 我这样想:先在 MySQL 中执行 Load data,然后 show warnings 不就 可以找到问题所在了吗?

mysql> truncate table ip_to_country;

mysql>mysql> load data infile "d:/ip_to_country.csv"          replace into table ip_to_country          character set gb2312          fields terminated by "," enclosed by ""          lines terminated by "/r/n";

ERROR 1262 (01000): Row 6737 was truncated; it contained more data than there were input columns

晕,又出现个拦路虎:ERROR 1262 (01000): Row 6737 was truncated; it contained more data than there were input columns. 最后发现问题是 sql_mode 的问题。

mysql> show variables like '%sql_mode%';

+---------------+----------------------------------------------------------------+| Variable_name | Value                                                          |+---------------+----------------------------------------------------------------+| sql_mode      | strict_trans_tables,no_auto_create_user,no_engine_substitution |+---------------+----------------------------------------------------------------+

mysql> set sql_mode='no_auto_create_user,no_engine_substitution';

把 strict_trans_tables 从 sql_mode 中去掉,再次执行 MySQL Load data

mysql>  load data infile "d:/ip_to_country.csv"               replace into table ip_to_country               character set gb2312               fields terminated by "," enclosed by ""               lines terminated by "/r/n";

Query OK, 65290 rows affected, 15 warnings (0.63 sec)Records: 65290  Deleted: 0  Skipped: 0  Warnings: 15

接下来,用 MySQL show warnings 命令,来找警告的详细描述:

mysql> show warnings;

+---------+------+-------------------------------------------------------------------------------+| Level   | Code | Message                                                                       |+---------+------+-------------------------------------------------------------------------------+| Warning | 1262 | Row 6737 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 6817 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 6914 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 6916 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 6918 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 6988 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 7028 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 7226 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 7569 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 7791 was truncated; it contained more data than there were input columns  || Warning | 1262 | Row 47856 was truncated; it contained more data than there were input columns || Warning | 1262 | Row 47885 was truncated; it contained more data than there were input columns || Warning | 1262 | Row 49331 was truncated; it contained more data than there were input columns || Warning | 1262 | Row 49539 was truncated; it contained more data than there were input columns || Warning | 1262 | Row 49547 was truncated; it contained more data than there were input columns |+---------+------+-------------------------------------------------------------------------------+

根据行号 Row 6737 到 ip_to_country.csv 中查看,发现果然有问题,国家中文名中间多了个逗号 “,”

1089579216,1089579223,VI,VIR,维京群岛,美国

看来,需要在表 ip_to_country 中再增加一列,来存放多出的内容。于是修改表结构:

mysql> alter table ip_to_country add column cname4 varchar(50) null;

再次执行 mysql load data,数据顺利导入。这时仍有警告,这些警告是因为 文件中的大部分数据行只有 5 列,而表中有 6 列,因此 MySQL 才 Warning。

把表 ip_to_country 的字符集改为 utf8,看有没有乱码:

truncate table ip_to_country;alter table ip_to_country default character set utf8;alter table ip_to_country convert to character set utf8;

再次,执行 MySQL Load data 命令:

mysql>  load data infile "d:/ip_to_country.csv"            replace into table ip_to_country            character set gb2312            fields terminated by "," enclosed by ""            lines terminated by "/r/n";

Query OK, 65290 rows affected, 65275 warnings (0.64 sec)Records: 65290  Deleted: 0  Skipped: 0  Warnings: 65275

怀着激动的心情,select:

mysql> select * from ip_to_country where cname4 is not null limit 10;

+------------+------------+--------+--------+----------+--------+| ip1        | ip2        | cname1 | cname2 | cname3   | cname4 |+------------+------------+--------+--------+----------+--------+| 1089579216 | 1089579223 | VI     | VIR    | 维京群岛 | 美国   || 1093062144 | 1093062399 | VI     | VIR    | 维京群岛 | 美国   || 1097896192 | 1097897215 | VI     | VIR    | 维京群岛 | 美国   || 1097947136 | 1097949183 | VI     | VIR    | 维京群岛 | 美国   || 1097951232 | 1097953279 | VI     | VIR    | 维京群岛 | 美国   || 1101625344 | 1101625407 | VI     | VIR    | 维京群岛 | 美国   || 1101971072 | 1101971079 | VI     | VIR    | 维京群岛 | 美国   || 1113864768 | 1113864783 | VI     | VIR    | 维京群岛 | 美国   || 1119428608 | 1119432703 | VI     | VIR    | 维京群岛 | 美国   || 1123590144 | 1123594239 | VI     | VIR    | 维京群岛 | 美国   |+------------+------------+--------+--------+----------+--------+

可见,MySQL load data infile 指令,可以实现不同字符集之间的转换。

总结:

show warnings;是不错的方法

在导入文件时

SET character_set_database=gbk是更不错的方法

mysql导入文件的经验文章很不错的相关推荐

  1. mysql导入文件出现Data truncated for column 'xxx' at row 1的原因

    mysql导入文件的时候很容易出现"Data truncated for column 'xxx' at row x",其中字符串里的xxx和x是指具体的列和行数. 有时候,这是因 ...

  2. mysql 导入文件提示 --secure-file-priv option 问题

    MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option s ...

  3. mysql 导入文件夹_MySQL-导入与导出

    CSV文件导入MySQL LOAD DATA INFILE语句允许您从文本文件读取数据,并将文件的数据快速导入数据库的表中. 导入文件操作之前,需要准备以下内容: 一.将要导入文件的数据对应的数据库表 ...

  4. linux中mysql导入文件,linux下mysql导入sql文件命令

    Linux下我们提供导入sql文件可以得到数据.下面由学习啦小编为大家整理了linux下mysql导入sql文件命令的相关知识,希望对大家有帮助! linux的mysql导入sql文件命令详解 lin ...

  5. mysql导入文件_mysql导入txt文件

    1.首先在命令行启动mysql net start mysql 2.登录MySQL(建议使用非root用户) mysql --local-infile=1 -u one -p 3.创建数据库 如 CR ...

  6. mysql导入文件靠近 出错_同望 WECOST-FAQ问题汇总

    128.问:板式橡胶支座如何进行"个数"与"dm3"的工程量换算? 答:(1)按结构型式分为: a.普通板式橡胶支座区分为矩形板式橡胶支座(代号GJZ).圆形板 ...

  7. MySQL导入sql 文件的5大步骤

    以下的文章主要介绍的是MySQL导入sql 文件,即MySQL数据库导入导出sql 文件的实际操作步骤,我们主要是将其分成5大步骤对其进行讲述,如果你对其有兴趣的话你就可以点击以下的文章进行观看了. ...

  8. mysql导入dat文件_从零开始学习 MySQL 系列--索引、视图、导入和导出

    前言 上篇文章我们学习了数据库和数据表操作语句,今天我们学习下数据库索引,视图,导入和导出的知识. 作为基础篇,不会涉及到关于索引和视图的高级应用和核心概念,但是基本操作大家会了解,尤其是关于索引的内 ...

  9. mysql 导入导出.sql文件

    备份数据库(包含全部表和全部存储过程): C:\Documents and Settings\Administrator>mysqldump -h localhost -u root -p -R ...

最新文章

  1. jq常用过滤器_Jquery过滤器
  2. python 豆瓣评论数据分析_Python爬虫实战案例:豆瓣影评大数据分析报告之网页分析...
  3. c++primer 5th第15章基础、课后习题自己解析、心得体会等
  4. 使用单独的解决方案(类库)来开发DNN的模块-C#版本(2)
  5. Rocksdb的优劣及应用场景分析
  6. Q-学习,马克尔决策过程:强化学习
  7. 2000以内!一加Nord 2渲染图曝光:搭载联发科天玑1200
  8. Sublime Text 3 Key
  9. 计算机网络性能(1)
  10. 设置域用户帐户密码策略
  11. 妙味课堂原创JavaScript视频教程基础+提高+项目
  12. css 字体压缩 fonttools
  13. 十大排序算法-桶排序(c语言实现)
  14. 滑动窗口协议如何实现流量控制
  15. 最新windows7旗舰版密钥
  16. 聊聊请教技术问题的正确方式
  17. 重启网卡提示Bringing up interface eth0
  18. CityMaker学习教程03 数据的导入
  19. java indexeddb_初探IndexedDB
  20. 亚信AX88179A千兆网卡芯片,支持switch联网。

热门文章

  1. java swing 模糊查找_Java Swing 编程 JComboBox 实现模糊查找功能。
  2. HDU6741秦皇岛CCPC2019MUV LUV UNLIMITED(博弈)
  3. r语言 四格画图_临度科研|数据统计的理解和运用(四)列联表之卡方检验
  4. C++之函数重载重铸版
  5. 正达信通丨【功能介绍】ZedaIoT物联网平台的视频监控设置说明
  6. 数据结构-树,二叉树,森林
  7. JAVA使用魔法数字
  8. SDUT 2022 Winter Individual Contest - D(K)
  9. ‘SchemaItem‘ object, such as a ‘Column‘ or a ‘Constraint‘ expected, go <class ‘int‘>
  10. Fatal: Failed to generate ABI binding: 5:9: expected ‘IDENT‘, found ‘.‘