win10中,在命令行情况下,Mysql显示各个端所使用的字符集命令:
Microsoft Windows [版本 10.0.16299.64]
(c) 2017 Microsoft Corporation。保留所有权利。
C:\Windows\system32>mysql -u root -proot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database day28;
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> use day28;
Database changed
mysql>
mysql> #用户表
mysql> CREATE TABLE S_User(
-> userID INT NOT NULL AUTO_INCREMENT, #主键ID
-> userName VARCHAR(50) NULL, #用户姓名
-> loginName VARCHAR(50) NULL, #登录名
-> loginPwd VARCHAR(50) NULL, #密码#
-> gender VARCHAR(10) NULL, #性别(例如:男,女)
-> birthday VARCHAR(50) NULL, #出生日期
-> education VARCHAR(20) NULL, #学历(例如:研究生、本科、专科、高中)
-> telephone VARCHAR(50) NULL, #电话
-> hobby VARCHAR(20) NULL, #兴趣爱好(例如:体育、旅游、逛街)
-> path VARCHAR(500) NULL, #上传路径(path路径)
-> filename VARCHAR(100) NULL, #上传文件名称(文件名)
-> remark VARCHAR(500) NULL, #备注
-> PRIMARY KEY (userID)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> #初始化数据:默认用户名和密码是admin
mysql> INSERT INTO s_user (userID,userName,loginName,loginPwd) VALUES (1,'超级管理员','admin','admin');
ERROR 1366 (HY000): Incorrect string value: '\xB3\xAC\xBC\xB6\xB9\xDC...' for column 'userName' at row 1 #客户端传来的数据的编码是gbk,而Mysql针对客户端默认使用的是utf8,所以会报错
mysql>
mysql> show variables like 'character%'; #显示各个端所使用的字符集,只对当前对话窗口有用
+--------------------------+---------------------------------------------------------+
| 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 | C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.01 sec)
mysql> set character_set_client=gbk; #设置Mysql针对客户端使用字符集为gbk编码,报错解决
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO s_user (userID,userName,loginName,loginPwd) VALUES (1,'超级管理员','admin','admin');
Query OK, 1 row affected (0.01 sec)
mysql> select * from s_user;
+--------+-----------------+-----------+----------+--------+----------+-----------+-----------+-------+------+----------+--------+
| userID | userName | loginName | loginPwd | gender | birthday | education | telephone | hobby | path | filename | remark |
+--------+-----------------+-----------+----------+--------+----------+-----------+-----------+-------+------+----------+--------+
| 1 | 瓒呯骇绠$悊鍛? | admin | admin | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------+-----------------+-----------+----------+--------+----------+-----------+-----------+-------+------+----------+--------+
1 row in set (0.00 sec) #因为Mysql针对默认结果集使用的是utf8编码,而我们的pc端使用的是gbk编码,所以会报错
mysql> set character_set_results=gbk; #设置Mysql针对数据返回结果集使用字符集为gbk编码,报错解决
Query OK, 0 rows affected (0.00 sec)
mysql> select * from s_user;
+--------+------------+-----------+----------+--------+----------+-----------+-----------+-------+------+----------+--------+
| userID | userName | loginName | loginPwd | gender | birthday | education | telephone | hobby | path | filename | remark |
+--------+------------+-----------+----------+--------+----------+-----------+-----------+-------+------+----------+--------+
| 1 | 超级管理员 | admin | admin | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------+------------+-----------+----------+--------+----------+-----------+-----------+-------+------+----------+--------+
1 row in set (0.01 sec)
mysql>