mysql 中文乱码的解决办法
I would not suggest Richies answer, because you are screwing up the data inside the database. You would not fix your problem but try to "hide" it and not being able to perform essential database operations with the crapped data.
If you encounter this error either the data you are sending is not UTF-8 encoded, or your connection is not UTF-8. First, verify, that the data source (a file, ...) really is UTF-8.
Then, check your database connection, you should do this after connecting:
SET NAMES 'utf8';
SET CHARACTER SET utf8;
Next, verify that the tables where the data is stored have the utf8 character set:
SELECT
`tables`.`TABLE_NAME`,
`collations`.`character_set_name`
FROM
`information_schema`.`TABLES` AS `tables`,
`information_schema`.`COLLATION_CHARACTER_SET_APPLICABILITY` AS `collations`
WHERE
`tables`.`table_schema` = DATABASE()
AND `collations`.`collation_name` = `tables`.`table_collation`
;
Last, check your database settings:
mysql> show variables like '%colla%';
mysql> show variables like '%charac%';
If source, transport and destination are UTF-8, your problem is gone;)
另外,安装完mysql之后,应该运行一下命令来设置编码为utf-8:
先在命令行运行:status查看编码是不是utf8。
CREATE DATABASE `test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
2、建表的时候
CREATE TABLE `database_user` (
`ID` varchar(40) NOT NULL default '',
`UserID` varchar(40) NOT NULL default '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
即建库和建表时都使用相同的编码格式。
In MySQL 4.1 and up, string data types include some features that you may not have encountered in working with versions of MySQL prior to 4.1:
MySQL interprets length specifications in character column definitions in character units. (Before MySQL 4.1, column lengths were interpreted in bytes.) This applies to
CHAR,VARCHAR, and theTEXTtypes.Column definitions for many string data types can include attributes that specify the character set or collation of the column. These attributes apply to the
CHAR,VARCHAR, theTEXTtypes,ENUM, andSETdata types:The
CHARACTER SETattribute specifies the character set, and theCOLLATEattribute specifies a collation for the character set. For example:CREATE TABLE t
(
c1 VARCHAR(20) CHARACTER SET utf8,
c2 TEXT CHARACTER SET latin1 COLLATE latin1_general_cs
);This table definition creates a column named
c1that has a character set ofutf8with the default collation for that character set, and a column namedc2that has a character set oflatin1and a case-sensitive collation.The rules for assigning the character set and collation when either or both of the
CHARACTER SETandCOLLATEattributes are missing are described in Section 10.1.3.4, “Column Character Set and Collation”.CHARSETis a synonym forCHARACTER SET.Specifying the
CHARACTER SET binaryattribute for a character data type causes the column to be created as the corresponding binary data type:CHARbecomesBINARY,VARCHARbecomesVARBINARY, andTEXTbecomesBLOB. For theENUMandSETdata types, this does not occur; they are created as declared. Suppose that you specify a table using this definition:CREATE TABLE t
(
c1 VARCHAR(10) CHARACTER SET binary,
c2 TEXT CHARACTER SET binary,
c3 ENUM('a','b','c') CHARACTER SET binary
);The resulting table has this definition:
CREATE TABLE t
(
c1 VARBINARY(10),
c2 BLOB,
c3 ENUM('a','b','c') CHARACTER SET binary
);The
ASCIIattribute is shorthand forCHARACTER SET latin1.The
UNICODEattribute is shorthand forCHARACTER SET ucs2.The
BINARYattribute is shorthand for specifying the binary collation of the column character set. In this case, sorting and comparison are based on numeric character values. (Before MySQL 4.1,BINARYcaused a column to store binary strings and sorting and comparison were based on numeric byte values. This is the same as using character values for single-byte character sets, but not for multibyte character sets.)
Character column sorting and comparison are based on the character set assigned to the column. (Before MySQL 4.1, sorting and comparison were based on the collation of the server character set.) For the
CHAR,VARCHAR,TEXT,ENUM, andSETdata types, you can declare a column with a binary collation or theBINARYattribute to cause sorting and comparison to use the underlying character code values rather than a lexical ordering.
Section 10.1, “Character Set Support”, provides additional information about use of character sets in MySQL.
[NATIONAL] CHAR[(M)] [CHARACTER SETcharset_name] [COLLATEcollation_name]A fixed-length string that is always right-padded with spaces to the specified length when stored.
Mrepresents the column length in characters. The range ofMis 0 to 255. IfMis omitted, the length is 1.NoteTrailing spaces are removed when
CHARvalues are retrieved.Before MySQL 5.0.3, a
CHARcolumn with a length specification greater than 255 is converted to the smallestTEXTtype that can hold values of the given length. For example,CHAR(500)is converted toTEXT, andCHAR(200000)is converted toMEDIUMTEXT. However, this conversion causes the column to become a variable-length column, and also affects trailing-space removal.In MySQL 5.0.3 and later, a
CHARlength greater than 255 is illegal and fails with an error:mysql>
CREATE TABLE c1 (col1 INT, col2 CHAR(500));
ERROR 1074 (42000): Column length too big for column 'col' (max = 255);
use BLOB or TEXT insteadCHARis shorthand forCHARACTER.NATIONAL CHAR(or its equivalent short form,NCHAR) is the standard SQL way to define that aCHARcolumn should use some predefined character set. MySQL 4.1 and up usesutf8as this predefined character set. Section 10.1.3.6, “National Character Set”.The
CHAR BYTEdata type is an alias for theBINARYdata type. This is a compatibility feature.MySQL permits you to create a column of type
CHAR(0). This is useful primarily when you have to be compliant with old applications that depend on the existence of a column but that do not actually use its value.CHAR(0)is also quite nice when you need a column that can take only two values: A column that is defined asCHAR(0) NULLoccupies only one bit and can take only the valuesNULLand''(the empty string).[NATIONAL] VARCHAR(M) [CHARACTER SETcharset_name] [COLLATEcollation_name]A variable-length string.
Mrepresents the maximum column length in characters. In MySQL 5.0, the range ofMis 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in MySQL 5.0.3 and later. The effective maximum length of aVARCHARin MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. For example,utf8characters can require up to three bytes per character, so aVARCHARcolumn that uses theutf8character set can be declared to be a maximum of 21,844 characters. See Section D.7.4, “Limits on Table Column Count and Row Size”.MySQL stores
VARCHARvalues as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. AVARCHARcolumn uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.NoteBefore 5.0.3, trailing spaces were removed when
VARCHARvalues were stored, which differs from the standard SQL specification.Prior to MySQL 5.0.3, a
VARCHARcolumn with a length specification greater than 255 is converted to the smallestTEXTtype that can hold values of the given length. For example,VARCHAR(500)is converted toTEXT, andVARCHAR(200000)is converted toMEDIUMTEXT. However, this conversion affects trailing-space removal.VARCHARis shorthand forCHARACTER VARYING.NATIONAL VARCHARis the standard SQL way to define that aVARCHARcolumn should use some predefined character set. MySQL 4.1 and up usesutf8as this predefined character set. Section 10.1.3.6, “National Character Set”.NVARCHARis shorthand forNATIONAL VARCHAR.-
The
BINARYtype is similar to theCHARtype, but stores binary byte strings rather than nonbinary character strings.Mrepresents the column length in bytes. -
The
VARBINARYtype is similar to theVARCHARtype, but stores binary byte strings rather than nonbinary character strings.Mrepresents the maximum column length in bytes. -
A
BLOBcolumn with a maximum length of 255 (28 − 1) bytes. EachTINYBLOBvalue is stored using a 1-byte length prefix that indicates the number of bytes in the value. TINYTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 255 (28 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachTINYTEXTvalue is stored using a 1-byte length prefix that indicates the number of bytes in the value.-
A
BLOBcolumn with a maximum length of 65,535 (216 − 1) bytes. EachBLOBvalue is stored using a 2-byte length prefix that indicates the number of bytes in the value.An optional length
Mcan be given for this type. If this is done, MySQL creates the column as the smallestBLOBtype large enough to hold valuesMbytes long. TEXT[(M)] [CHARACTER SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 65,535 (216 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachTEXTvalue is stored using a 2-byte length prefix that indicates the number of bytes in the value.An optional length
Mcan be given for this type. If this is done, MySQL creates the column as the smallestTEXTtype large enough to hold valuesMcharacters long.-
A
BLOBcolumn with a maximum length of 16,777,215 (224 − 1) bytes. EachMEDIUMBLOBvalue is stored using a 3-byte length prefix that indicates the number of bytes in the value. MEDIUMTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 16,777,215 (224 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachMEDIUMTEXTvalue is stored using a 3-byte length prefix that indicates the number of bytes in the value.-
A
BLOBcolumn with a maximum length of 4,294,967,295 or 4GB (232 − 1) bytes. The effective maximum length ofLONGBLOBcolumns depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGBLOBvalue is stored using a 4-byte length prefix that indicates the number of bytes in the value. LONGTEXT [CHARACTER SETcharset_name] [COLLATEcollation_name]A
TEXTcolumn with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. The effective maximum length is less if the value contains multibyte characters. The effective maximum length ofLONGTEXTcolumns also depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGTEXTvalue is stored using a 4-byte length prefix that indicates the number of bytes in the value.ENUM('value1','value2',...) [CHARACTER SETcharset_name] [COLLATEcollation_name]An enumeration. A string object that can have only one value, chosen from the list of values
',value1'',value2'...,NULLor the special''error value.ENUMvalues are represented internally as integers.An
ENUMcolumn can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) A table can have no more than 255 unique element list definitions among itsENUMandSETcolumns considered as a group. For more information on these limits, see Section D.7.5, “Limits Imposed by .frm File Structure”.SET('value1','value2',...) [CHARACTER SETcharset_name] [COLLATEcollation_name]A set. A string object that can have zero or more values, each of which must be chosen from the list of values
',value1'',value2'...SETvalues are represented internally as integers.A
SETcolumn can have a maximum of 64 distinct members. A table can have no more than 255 unique element list definitions among itsENUMandSETcolumns considered as a group. For more information on this limit, see Section D.7.5, “Limits Imposed by .frm File Structure”.
[mysql]
auto-rehash
add no-auto-rehash to disable auto completion.
To enable autocomplete within the MySQL prompt type:
mysql> \#
After that you can type:
mysql> describe someTableW[TAB]
To get:
mysql> describe someTableWithRidiculousLongName
mysql 中文乱码的解决办法的更多相关文章
- FIREDAC连MYSQL中文乱码的解决办法
FIREDAC连MYSQL中文会乱码,因为字符集的原因,字符集设为gb2312以后,不再乱码. if SameText(DatabaseParams.driveId, 'MySQL') then fd ...
- C#中WebClient使用DownloadString中文乱码的解决办法
原文:C#中WebClient中文乱码的解决办法 第一次尝试: string question = textBox1.Text.ToString(); WebClient client= new We ...
- 详解get请求和post请求参数中文乱码的解决办法
首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...
- Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧)
干货:Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧) [解决办法]: 菜单栏中[File]->[Reload As E ...
- IDEA使用maven构建时控制台中文乱码的解决办法
使用maven clean install 项目时控制台中文乱码,解决办法如下: Setting->maven->runner VMoptions: -Dfile.encoding=UTF ...
- resin后台输出中文乱码的解决办法!
resin后台输出中文乱码的解决办法! 学习了:https://blog.csdn.net/kobeguang/article/details/34116429 编辑conf/resin.con文件: ...
- php使用curl获取文本出现中文乱码的解决办法
在使用php的curl获取远程html文本时出现了中文乱码. 解决办法的代码如下: $url = "www.ecjson.com";//获取页面内容$ch = curl_init( ...
- get请求和post请求参数中文乱码的解决办法
get请求参数中文乱码的解决办法 在tomcat的server.xml里的Connector加个URIEncoding="UTF-8",把 <Connector connec ...
- python 数据库操作产生中文乱码的解决办法
1.执行python mysql数据库查询操作时,产生中文乱码 #!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb db = MySQLd ...
随机推荐
- 重置mysql密码
如何修改mysql root密码 忘记MySQL ROOT密码是在MySQ使用中很常见的问题,可是有很多朋友并不会重置ROOT密码,那叫苦啊,特写此文章与大家交流: 1.编辑MySQL的配置文件:my ...
- JUnit测试工具在项目中的用法
0:33 2013/6/26 三大框架整合时为什么要对项目进行junit测试: |__目的是测试配置文件对不对,能跑通就可以进行开发了 具体测试步骤: |__1.对hibernate进行测试 配置hi ...
- collectionView布局原理及瀑布流布局方式--备用
最近学习到了瀑布流的实现方法,瀑布流的实现方式有多种,这里应用collectionView来重写其UICollectionViewLayout进行布局是最为简单方便的.但再用其布局之前必须了解其布局原 ...
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...
- 简单方法打包.net程序集脱离framework
最近业余捣鼓monogame,自然而然就关注到了.net程序脱离framework发布的问题上了, 度娘,谷歌娘 都经过一番查找,无非分为如下几类方法: 1.直接使用mono运行时,附带 bin.li ...
- 使用text-overflow:ellipsis对溢出文本显示省略号有两个好处
使用text-overflow:ellipsis对溢出文本显示省略号有两个好处,一是不用通过程序限定字数:二是有利于SEO.需要使用对对溢出文本显示省略号的通常是文章标题列表,这样处理对搜索引擎更友好 ...
- Power Designer 使用技巧总结
1.设置主键自增 在表的属性界面---选择column---双击主键: 2. 为脚本添加注释: 在表的属性界面---选择column分别进行下列设置:
- linux-0.11内核 任务的堆栈切换
http://blog.163.com/di_yang@yeah/blog/static/86118492201212534924900/ 一直缠绕的两个问题:怎样标识的内核栈与用户栈?如何在内核态堆 ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- STL--自定义类型的排序
STL的排序太坑了,尤其是在VS2010上重载sort函数的第三个比较参数的时候. invalid operator < 这个错在写多关键字排序的时候就没有停止过. 本来想查书解决,结果各种重载 ...