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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks 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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles