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 theTEXT
types.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
, theTEXT
types,ENUM
, andSET
data types:The
CHARACTER SET
attribute specifies the character set, and theCOLLATE
attribute 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
c1
that has a character set ofutf8
with the default collation for that character set, and a column namedc2
that has a character set oflatin1
and a case-sensitive collation.The rules for assigning the character set and collation when either or both of the
CHARACTER SET
andCOLLATE
attributes are missing are described in Section 10.1.3.4, “Column Character Set and Collation”.CHARSET
is a synonym forCHARACTER SET
.Specifying the
CHARACTER SET binary
attribute for a character data type causes the column to be created as the corresponding binary data type:CHAR
becomesBINARY
,VARCHAR
becomesVARBINARY
, andTEXT
becomesBLOB
. For theENUM
andSET
data 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
ASCII
attribute is shorthand forCHARACTER SET latin1
.The
UNICODE
attribute is shorthand forCHARACTER SET ucs2
.The
BINARY
attribute 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,BINARY
caused 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
, andSET
data types, you can declare a column with a binary collation or theBINARY
attribute 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.
M
represents the column length in characters. The range ofM
is 0 to 255. IfM
is omitted, the length is 1.NoteTrailing spaces are removed when
CHAR
values are retrieved.Before MySQL 5.0.3, a
CHAR
column with a length specification greater than 255 is converted to the smallestTEXT
type 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
CHAR
length 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 insteadCHAR
is shorthand forCHARACTER
.NATIONAL CHAR
(or its equivalent short form,NCHAR
) is the standard SQL way to define that aCHAR
column should use some predefined character set. MySQL 4.1 and up usesutf8
as this predefined character set. Section 10.1.3.6, “National Character Set”.The
CHAR BYTE
data type is an alias for theBINARY
data 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) NULL
occupies only one bit and can take only the valuesNULL
and''
(the empty string).[NATIONAL] VARCHAR(
M
) [CHARACTER SETcharset_name
] [COLLATEcollation_name
]A variable-length string.
M
represents the maximum column length in characters. In MySQL 5.0, the range ofM
is 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 aVARCHAR
in 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,utf8
characters can require up to three bytes per character, so aVARCHAR
column that uses theutf8
character 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
VARCHAR
values as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. AVARCHAR
column 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
VARCHAR
values were stored, which differs from the standard SQL specification.Prior to MySQL 5.0.3, a
VARCHAR
column with a length specification greater than 255 is converted to the smallestTEXT
type 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.VARCHAR
is shorthand forCHARACTER VARYING
.NATIONAL VARCHAR
is the standard SQL way to define that aVARCHAR
column should use some predefined character set. MySQL 4.1 and up usesutf8
as this predefined character set. Section 10.1.3.6, “National Character Set”.NVARCHAR
is shorthand forNATIONAL VARCHAR
.-
The
BINARY
type is similar to theCHAR
type, but stores binary byte strings rather than nonbinary character strings.M
represents the column length in bytes. -
The
VARBINARY
type is similar to theVARCHAR
type, but stores binary byte strings rather than nonbinary character strings.M
represents the maximum column length in bytes. -
A
BLOB
column with a maximum length of 255 (28 − 1) bytes. EachTINYBLOB
value is stored using a 1-byte length prefix that indicates the number of bytes in the value. TINYTEXT [CHARACTER SET
charset_name
] [COLLATEcollation_name
]A
TEXT
column with a maximum length of 255 (28 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachTINYTEXT
value is stored using a 1-byte length prefix that indicates the number of bytes in the value.-
A
BLOB
column with a maximum length of 65,535 (216 − 1) bytes. EachBLOB
value is stored using a 2-byte length prefix that indicates the number of bytes in the value.An optional length
M
can be given for this type. If this is done, MySQL creates the column as the smallestBLOB
type large enough to hold valuesM
bytes long. TEXT[(
M
)] [CHARACTER SETcharset_name
] [COLLATEcollation_name
]A
TEXT
column with a maximum length of 65,535 (216 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachTEXT
value is stored using a 2-byte length prefix that indicates the number of bytes in the value.An optional length
M
can be given for this type. If this is done, MySQL creates the column as the smallestTEXT
type large enough to hold valuesM
characters long.-
A
BLOB
column with a maximum length of 16,777,215 (224 − 1) bytes. EachMEDIUMBLOB
value is stored using a 3-byte length prefix that indicates the number of bytes in the value. MEDIUMTEXT [CHARACTER SET
charset_name
] [COLLATEcollation_name
]A
TEXT
column with a maximum length of 16,777,215 (224 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachMEDIUMTEXT
value is stored using a 3-byte length prefix that indicates the number of bytes in the value.-
A
BLOB
column with a maximum length of 4,294,967,295 or 4GB (232 − 1) bytes. The effective maximum length ofLONGBLOB
columns depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGBLOB
value is stored using a 4-byte length prefix that indicates the number of bytes in the value. LONGTEXT [CHARACTER SET
charset_name
] [COLLATEcollation_name
]A
TEXT
column 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 ofLONGTEXT
columns also depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGTEXT
value 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
'...
,NULL
or the special''
error value.ENUM
values are represented internally as integers.An
ENUM
column 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 itsENUM
andSET
columns 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
'...
SET
values are represented internally as integers.A
SET
column can have a maximum of 64 distinct members. A table can have no more than 255 unique element list definitions among itsENUM
andSET
columns 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 ...
随机推荐
- 【Base64】JDK里面实现Base64的API
原文出处: 成熟的毛毛虫的博客 BASE64 编码是一种常用的字符编码,在很多地方都会用到.但base64不是安全领域下的加密解密算法.能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数 ...
- 制作按钮(Button)
按钮的核心作用 1.按钮能接收单击并触发响应事件. 2.按钮被单击时能同时触发多个响应事件. 3.按钮可以有普通.悬停.单击.禁用等多个状态的不同表现. 4.广泛的说,按钮的核心在于接收事件,任何可以 ...
- android报表图形引擎(AChartEngine)demo解析与源码
AchartEngine支持多种图表样式,本文介绍两种:线状表和柱状表. AchartEngine有两种启动的方式:一种是通过ChartFactory.get***View()方式来直接获取到view ...
- 项目中常用功能,如:流媒体、健康数据(步数等)等-b
整理iOS开发中使用的各种流媒体和常用的高级功能.由于时间关系,目前只写了一部分功能,全部都采用的是系统方法,没用第三方,截图如下: screen1.png screen2.png 个人比较懒,不爱多 ...
- ios阻止锁屏 --老代码,供参考
// Disable the idle timer [[UIApplication sharedApplication] setIdleTimerDisabled: YES]; // Or fo ...
- bp神经网络算法
对于BP神经网络算法,由于之前一直没有应用到项目中,今日偶然之时 进行了学习, 这个算法的基本思路是这样的:不断地迭代优化网络权值,使得输入与输出之间的映射关系与所期望的映射关系一致,利用梯度下降的方 ...
- zoj 3757&&3758
3757一个模拟题,简单,但容易错: 3758 大素数判定就行: #include<cstdio> #include<cstring> #include<algorith ...
- HDU4530+模拟
/* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorith ...
- GridView使用CommandField删除列实现删除时提示确认框
在.net2005提供的GridView中我们可以直接添加一个CommandField删除列完后在它的RowDeleting事件中完成删除 GridView在使用CommandField删除时弹出提示 ...
- M-JPEG和MPEG-4的区别 M-JPEG VS MPEG
http://blog.sina.com.cn/s/blog_4b357b300100gre9.html M-JPEG VS MPEG http://blog.csdn.net/bluesky_sun ...