MySQL 字符集相关问题

来源:互联网 发布:怎么ping端口有没有通 编辑:程序博客网 时间:2024/05/16 01:00

MySQL安装时指定的字符集为UTF8,但是当我向如下表插入汉字时,总是失败(错误字符)!

DROP TABLE IF EXISTS t_weather;create table t_weather(c_idint unsigned not null auto_increment,c_provincevarchar(20) not null,c_cityvarchar(20) not null,c_city_codechar(5) not null,c_temperature varchar(20) not null,c_datedate,primary key (c_id));INSERT INTO t_weather (c_province,c_city,c_city_code,c_temperature,c_date) VALUES('湖北', 'WuHan', '57494', '28-37','2011-7-29');INSERT INTO t_weather (c_province,c_city,c_city_code,c_temperature,c_date) VALUES('BeiJing', 'BeiJing', '54511', '23-27','2011-7-29');INSERT INTO t_weather (c_province,c_city,c_city_code,c_temperature,c_date) VALUES('ShangHai', 'ShangHai', '58367', '28-37','2011-7-29');INSERT INTO t_weather (c_province,c_city,c_city_code,c_temperature,c_date) VALUES('GunagDong', 'ShenZhen', '59493', '25-29','2011-7-29');INSERT INTO t_weather (c_province,c_city,c_city_code,c_temperature,c_date) VALUES('FuJian', 'XiaMen', '59134', '26-33','2011-7-29');


可能原因:操作系统默认的字符集是GBK,因而使用UltraEdit 编辑汉字时底层保存为GBK字符,当向MySQL插入时就报错了!

因而,改正方法为:将MySQL的默认字符集,改为GBK


查看编码:
show variables like 'collation_%';
show variables like 'character_set_%';

修改默认编码:

进入C:\Program Files\MySQL\MySQL Server 5.0 ,打开 my.ini

修改,default-character-set=gbk