天天看點

【Mysql】iconv 轉換字元集

<b>學習mysql字元集轉換的過程中,看到有些網絡資料有使用iconv 指令轉換字元集的,是以這裡學習一下,iconv 的使用!(ps 網絡中關于使用iconv 轉換mysqldump 出的檔案不靠譜,随後的文章會介紹)</b>

<b>iconv的用法:</b>

用法: iconv [選項...] [檔案...]

Convert encoding of given files from one encoding to another.

輸入/輸出格式規範:

  -f, --from-code=NAME       原始文本編碼

  -t, --to-code=NAME         輸出編碼

資訊:

  -l, --list                 列舉所有已知的字元集

輸出控制:

  -c                         從輸出中忽略無效的字元

  -o, --output=FILE          輸出檔案

  -s, --silent               suppress warnings

      --verbose              列印進度資訊

  -?, --help                 給出該系統求助清單

      --usage                給出簡要的用法資訊

  -V, --version              列印程式版本号

mysql&gt; use latin

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&gt; select * from ytab;

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

| name   | val            |

| yangql | 楊奇龍      | 

| cat    | 貓            | 

| lily   | lily           | 

| 32     | shengxiaonan32 | 

4 rows in set (0.00 sec)

<b>導出資料庫latin中的ytab表:</b>

[root@rac3 ~]# mysqldump -h127.0.0.1   -uroot  latin  ytab &gt; create_la_ytab1.sql                              

[root@rac3 ~]# cat create_la_ytab1.sql 

-- MySQL dump 10.11

-- Host: 127.0.0.1    Database: latin

-- ------------------------------------------------------

-- Server version       5.0.45

-- Table structure for table `ytab`

--

DROP TABLE IF EXISTS `ytab`;

CREATE TABLE `ytab` (

  `name` varchar(15) default NULL,

  `val` varchar(15) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- Dumping data for table `ytab`

LOCK TABLES `ytab` WRITE;

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

INSERT INTO `ytab` VALUES <b>('yangql','??¨?¥?é??'),('cat','???')</b>,('lily','lily'),('32','shengxiaonan32'); --出現亂碼

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

UNLOCK TABLES;

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

-- Dump completed on 2011-10-27 13:40:25

[root@rac3 ~]# iconv -t latin1 -f utf8 -c create_la_ytab1.sql &gt; ytab-latin.sql            

[root@rac3 ~]# cat ytab-latin.sql

INSERT INTO `ytab` VALUES<b> ('yangql','楊'),('cat',',</b>('lily','lily'),('32','shengxiaonan32');--引起部分資料丢失。。

[root@rac3 ~]# 

<b>note:生産環境慎用!</b>