Home Database Mysql Tutorial [转]如何对mysql中的字符进行编码转换

[转]如何对mysql中的字符进行编码转换

Jun 07, 2016 pm 03:14 PM
mysql how character source coding Convert conduct

来源:http://xiaoych.javaeye.com/blog/148704 算来我也是mysql的忠实用户了,从mysql 3 的时候就开始用mysql,直到现在开始使用5.1版本,看到mysql一点一点的变化,感觉mysql功能越来越强大,真是是我们这种用不起oracle用户的福音啊! 如果没有记错的话,

 

来源:http://xiaoych.javaeye.com/blog/148704

算来我也是mysql的忠实用户了,从mysql 3 的时候就开始用mysql,直到现在开始使用5.1版本,看到mysql一点一点的变化,感觉mysql功能越来越强大,真是是我们这种用不起oracle用户的福音啊!

如果没有记错的话,mysql4.0升级到4.1以后,就增加了字符集这个概念。从mysql4.0及以下的版本,迁移到mysql4.1及其以上的版本就会出现中文字符出现乱码的问题。由于以前mysql的编码是latin1 SW (好像是瑞典字符集——mysql是由瑞典人开发的,估计当时没有考虑国际化的问题),现在我们用的编码一般是gbk或者utf8。那么以前latin1编码的表中的中文怎么迁移到gbk或者utf8编码的表中呢?直接更改表的编码是没有任何作用的,参考了一些资料,说是mysql在更改表编码的时候,不会对表中现有数据进行转码。

网上有很多怎么进行转码的资料和程序,感觉都不是很方便。比较BT的就是用PHP,一行一行的从mysql的源表中读出来,再利用PHP进行转码,然后再查到目标表中。想想这样做确实有用。但是性能惨不忍睹……小表还可以这么做,要是上百万行记录的表,非等上几个小时不可,实在太浪费时间。

好了,前面都是废话,俺就贡献一下俺的研究吧:

首先,到mysql/bin 下面,利用mysqldump这个工具,执行以下命令:

mysql代码
mysqldump --u=root -p --default-character-set=latin1 --set-charset=utf8 --skip-opt --result-file=c:/mytable.sql mydb mytable 

其中:root 为数据库登录名, latin1 为源表(就是想进行转码的表)的编码, utf8 为想转换成的编码, c:/mytable.sql 为导出的数据的存放文件(临时用), mydb是源表所属的数据库(schema),mytable 就是源表名了

执行这条命令,会提示输入密码,输入正确的密码以后,就开始导出数据了。等到数据全部导出以后,可以用ue等工具打开,这时可以看到这些数据的编码已经转变了。

然后需要对这个文件进行一点点更改。在文件的最开头有一个建表语句。类似于:
mysql代码
CREATE TABLE `mytable` (  
  `tableid` bigint(20) unsigned NOT NULL,  
  `c1` int(10) unsigned NOT NULL default '0',  
  `c2` int(10) unsigned NOT NULL default '0',  
  PRIMARY KEY  (`tableid`)  
); 

注意看最后的分号,缺少了一点点东西:engine=myisam DEFAULT CHARSET=utf8 engine 和 charset 的意义地球人都知道啊... 将这一段加进去。结果可能是这样:

mysql代码
CREATE TABLE `mytable` (  
  `tableid` bigint(20) unsigned NOT NULL,  
  `c1` int(10) unsigned NOT NULL default '0',  
  `c2` int(10) unsigned NOT NULL default '0',  
  PRIMARY KEY  (`tableid`)  
) engine=myisam DEFAULT CHARSET=utf8; 


其中engine 和 charset 改成期望的东西,如:innodb gbk 等...

保存文件。(如果是用UE等工具即使文件大也不会等太久,如果用记事本打开的……恭喜你!)

这样就成功了一半了,剩下的工作只需要导入这个转好码的数据了。

将原来的那个表改名,一是为了备份,二是防止导入的时候说表已经存在。

然后还是进入mysql/bin 下面,运行:

mysql代码
mysql -u root -p mydb


输入密码以后程序开始工作,一段时间以后,新表就出来咯...

mission complete!  

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to build a SQL database How to build a SQL database Apr 09, 2025 pm 04:24 PM

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.

See all articles