mysql iconv_file和iconv使用小记

用mysqldump导出数据库时,如果客户端字符集和数据库字符集不同,则导出的文件中中文可能为乱码。

[zzx@localhost ~]$ mysql -uroot

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 47

Server version: 5.0.45-community-log MySQL Community Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show create table a;

+-------+---------------------------------------------------------------------------------------+

| Table | Create Table                                                                          |

+-------+---------------------------------------------------------------------------------------+

| a     | CREATE TABLE `a` (

`name` char(10) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=gbk |

+-------+---------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

从上面可以看出,表a的字符集为gbk。

[zzx@localhost ~]$ mysqldump -uroot --default-character-set=utf8 test a >a.txt

[zzx@localhost ~]$ more a.txt

-- MySQL dump 10.11

--

。。。。。。

DROP TABLE IF EXISTS `a`;

CREATE TABLE `a` (

`name` char(10) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=gbk;

--

-- Dumping data for table `a`

--

LOCK TABLES `a` WRITE;

/*!40000 ALTER TABLE `a` DISABLE KEYS */;

INSERT INTO `a` VALUES ('涓浗'),('涓浗');

/*!40000 ALTER TABLE `a` ENABLE KEYS */;

UNLOCK TABLES;

/

。。。。。。

-- Dump completed on 2008-05-16 10:50:06

此时,用file命令可以查看导出文件的字符集:

[zzx@localhost ~]$ file -i a.txt

a.txt: text/plain; charset=utf-8

然后用iconv可以将此文件转换为其他字符集的文件:

[zzx@localhost ~]$ iconv -f utf8 -t gbk a.txt >b.txt

[zzx@localhost ~]$ more b.txt

。。。。。。

DROP TABLE IF EXISTS `a`;

CREATE TABLE `a` (

`name` char(10) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=gbk;

--

-- Dumping data for table `a`

--

LOCK TABLES `a` WRITE;

/*!40000 ALTER TABLE `a` DISABLE KEYS */;

INSERT INTO `a` VALUES ('中国'),('中国');

/*!40000 ALTER TABLE `a` ENABLE KEYS */;

UNLOCK TABLES;

/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

。。。。。。

-- Dump completed on 2008-05-16 10:54:12

果然,数据中的乱码已经变成了中文‘中国’。

但是转换过程中也有一些困惑,比如,将utf8转换为Latin1的时候会报错:

[zzx@localhost ~]$ iconv -f utf8 -t latin1 a.txt >b.txt

iconv: illegal input sequence at position 1034

感觉utf8应该是任何字符集的超级,它怎么会无法转换为latin1呢?试了其他的字符集,也是同样问题,似乎只能在utf8和gbk或者gb2312之间转换,郁闷!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值