标签:
背景:
写一个数据DB操作时,日志报Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (gbk_chinese_ci,COERCIBLE) for operation ‘=‘ 错误;
原因:
1、插入式代码中的数据类型为GBK;
2、DB建表时忘记修改默认的编码格式(默认为 Collation: latin1_swedish_ci 格式);
1、2结合之后,插入数据编码错误;
定位方法:
1、查看DB表的字段编码 show full columns from tablename;
2、检查struts编码
修改方法:
1、修改数据库DB的字段设置;
mysql> SHOW VARIABLES LIKE ‘character_set_%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE ‘collation_%‘;
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
依次执行:
set character_set_client =gbk;
set character_set_connection =gbk;
set character_set_database =gbk;
set character_set_results =gbk;
set character_set_server =gbk;
set character_set_system =gbk; --此处utf-8也可以
然后执行:
SET collation_server = gbk_chinese_ci
SET collation_database = gbk_chinese_ci
SET collation_connection =gbk_chinese_ci
标签:
原文地址:http://www.cnblogs.com/carterzhang/p/4374068.html