mysql 无法插入中文
https://democome.com
看网上说是吧my.ini中的这段代码改成utf8
1 [mysql] 2 3 default-character-set=utf8 4 5 6 # SERVER SECTION 7 # ---------------------------------------------------------------------- 8 # 9 # The following options will be read by the MySQL Server. Make sure that 10 # you have installed the server correctly (see above) so it reads this 11 # file. 12 # 13 [mysqld] 14 15 # The TCP/IP Port the MySQL Server will listen on 16 port=3306 17 18 19 #Path to installation directory. All paths are usually resolved relative to this. 20 basedir="C:/Program Files/MySQL/MySQL Server 5.0/" 21 22 #Path to the database root 23 datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/" 24 25 # The default character set that will be used when a new schema or table is 26 # created and no character set is defined 27 character-set-server=utf8
改成utf8之后还是不行,后来找到一篇文章帮助解决
http://database.51cto.com/art/201107/279004.htm
MySQL数据库默认编码已经是utf8了, default-character-set = utf8,可是向数据库中表中插入中文时,却老是出现 ....\xB5\xA5\xD1\xA1 for column...这样的错误,经过查阅网上类型的错误后,发现一个很奇妙的现象,接下来我们就来开始介绍。
一、在安装数据库时就设定字符集编码为utf8的环境下
1.在当前字符集编码为:
- Server characterset: utf8
- Db characterset: utf8
- Client characterset: utf8
- Conn. characterset: utf8
下创建数据库:
(1)插入中文数据,操作不能成功。
(2)修改字符集编码为:
- Server characterset: utf8
- Db characterset: utf8
- Client characterset: gbk
- Conn. characterset: gbk
插入中文数据,显示正常。
二、在安装数据库时就设定字符集编码为gbk的环境下
1.当前字符集编码是:
- Server characterset: gbk
- Db characterset: gbk
- Client characterset: gbk
- Conn. characterset: gbk
下创建数据,插入中文数据,操作正常,显示正常。
2.当前字符集编码为utf8:
- Server characterset: gbk
- Db characterset: utf8
- Client characterset: utf8
- Conn. characterset: utf8
3.当前字符集编码为utf8:
- Server characterset: gbk
- Db characterset: gbk
- Client characterset: utf8
- Conn. characterset: utf8
插入中文数据,操作不能执行。
结论:要想能向数据库正常插入中文,必须满足以下条件:
1.要把字符集设定为能支持中文的gbk或则utf8;
2.把数据库的Client Characterset和Conn. Characterset的字符集都设定为gbk;
根据上面的提示 ,我更改了:
- SET character_set_client=gbk;
- SET character_set_connection=gbk;
之后再尝试想数据库插入中文,这时没有出错,能插入可是查询出来是乱码。
最后再更改;
- SET character_set_results=gbk;
这样之后,所有的问题都解决了,能正常插入也能正常显示了。
另附查看字符集命令
5.查看字符集设置
mysql> show variables like 'collation_%';
mysql> show variables like 'character_set_%';
修改字符集
mysql> set character_set_client=utf8;
mysql> set character_set_connection=utf8;
mysql> set character_set_database=utf8;
mysql> set character_set_results=utf8;
mysql> set character_set_server=utf8;
mysql> set character_set_system=utf8;
mysql> set collation_connection=utf8;
mysql> set collation_database=utf8;
mysql> set collation_server=utf8;