首页 > 代码库 > MySQL字符编码问题,Incorrect string value
MySQL字符编码问题,Incorrect string value
Incorrect string value: ‘\xD0\xC2\xC8A\xBEW‘ for column ‘ctnr‘ at row 1
MySQL字符集相关參数:
character_set_server : 服务器字符集
collation_server : 服务器校对规则
character_set_database : 默认数据库的字符集
collation_database : 默认数据库的校对规则
character_set_client:server使用该变量取得链接中客户端的字符集
character_set_connection:server将客户端的query从character_set_client转换到该变量指定的字符集。
character_set_results:server发送结果集或返回错误信息到client之前应该转换为该变量指定的字符集
有两个语句能够设置连接字符集。例如以下:
A
SET NAMES ‘charset_name‘ 相当于以下三句:
mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET character_set_connection = x; #这个也设置了collation_connection的默认值x
B
SET CHARACTER SET charset_name 相当于以下三句:
mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET collation_connection = @@collation_database;
character_set_results为NULL时,server对返回结果集不做不论什么转换
mysql> SET character_set_results = NULL;
由于字符集编码引起的问题在pg上报的错是:invalid byte sequence for encoding "UTF8"。详细见參考
http://blog.csdn.net/beiigang/article/details/39582583
1
mysql> show variables like ‘%character_set%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
2
mysql> create table tb_tt (id int, ctnr varchar(60));
Query OK, 0 rows affected (0.06 sec)
3
mysql> show create table tb_tt;
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
| tb_tt | CREATE TABLE `tb_tt` (
`id` int(11) DEFAULT NULL,
`ctnr` varchar(60) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
1 row in set (0.00 sec)
4
mysql> insert into tb_tt(id,ctnr) values(1,‘新華網‘);
Query OK, 1 row affected (0.02 sec)
5
mysql> select * from tb_tt;
+------+--------+
| id | ctnr |
+------+--------+
| 1 | 新華網 |
+------+--------+
1 row in set (0.02 sec)
6
mysql> set names ‘UTF8‘;
Query OK, 0 rows affected (0.00 sec)
7
mysql> show variables like ‘%character_set%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
8
mysql> insert into tb_tt(id,ctnr) values(2,‘新華網‘);
ERROR 1366 (HY000): Incorrect string value: ‘\xD0\xC2\xC8A\xBEW‘ for column ‘ctnr‘ at row 1
9
If you change the default character set or collation for a database,
stored routines that use the database defaults must be dropped and
recreated so that they use the new defaults. (In a stored routine,
variables with character data types use the database defaults if the
character set or collation are not specified explicitly. See [HELP
CREATE PROCEDURE].)
參考:
http://dev.mysql.com/doc/refman/5.5/en/globalization.html
http://dev.mysql.com/doc/refman/5.5/en/alter-database.html
-----------------
blog.csdn.net/beiigang
MySQL字符编码问题,Incorrect string value