往mysql插入中文數據時出現亂碼,如下:
mysql> select * from test; +------+-----------+ | id | name | +------+-----------+ | 1 | 鐜嬪煿鍧?| | 2 | 鐜嬫檽宄?| | 3 | 寮犲皬闆?| | 4 | 鐜嬮洩宄?| | 5 | 闄嗕簯 | | 6 | 妗冭姳濂?| | 1 | demo1 | | 2 | demo2 | | 3 | demo3 |
+------+-----------+
但是在應用程序中卻可以正常的現實數據
1李曉峰 2王曉峰 3張小雪 4王雪峰 5陸雲 6桃花女 1demo1 2demo2 3demo3
為了在客戶端可以正常的顯示中文,可以修改下編碼集,如下:
輸入 show variables like 'char%';
mysql> show variables like 'char%'; +--------------------------+-----------------------------+ | Variable_name | Value | +--------------------------+-----------------------------+ | character_set_client | utf8 | | character_set_connection | gbk | | character_set_database | gbk | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | gbk | | character_set_system | utf8 | | character_sets_dir | D:\mysql6.0\share\charsets\ | +--------------------------+-----------------------------+ 8 rows in set (0.00 sec)
可以看到character_set_client 和character_set_results的編碼都為utf8,現在把它們都修改為gbk
mysql> set character_set_client=gbk; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_results=gbk; Query OK, 0 rows affected (0.00 sec)
再使用 show variables like 'char%';查詢
mysql> show variables like 'char%'; +--------------------------+-----------------------------+ | Variable_name | Value | +--------------------------+-----------------------------+ | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | gbk | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | gbk | | character_set_system | utf8 | | character_sets_dir | D:\mysql6.0\share\charsets\ | +--------------------------+-----------------------------+
再查看數據庫中的內容
mysql> select * from test; +------+--------+ | id | name | +------+--------+ | 1 | 李曉峰 | | 2 | 王曉峰 | | 3 | 張小雪 | | 4 | 王雪峰 | | 5 | 陸雲 | | 6 | 桃花女 | | 1 | demo1 | | 2 | demo2 | | 3 | demo3 | +------+--------+ 9 rows in set (0.00 sec)