MySQL安装时指定的字符集为UTF8,但是当我向如下表插入汉字时,总是失败(错误字符)!
DROP TABLE IF EXISTS t_weather;
create table t_weather
(c_id int unsigned not null auto_increment,c_province varchar(20) not null,c_city varchar(20) not null,c_city_code char(5) not null,c_temperature varchar(20) not null,c_date date,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