我们有一个MySQL5的数据库,要往里边插入汉字,可是插入的时候出错了,这是为什么?我对此展开了调查。。
首先进入MySQL
>mysql -u root -p
Password:******
mysql>
然后建立一个数据库,
mysql>create database test1 default charset gbk;
没有问题,
mysql>use test1;
然后,把sql语句保存到f:/SQL/a3.sql中
sql语句如下:
DROP TABLE IF EXISTS `flow`;
CREATE TABLE `flow` (
`FID` INT(5) NOT NULL AUTO_INCREMENT,
`content` char(100) DEFAULT NULL,
`stage` char(50) DEFAULT NULL,
PRIMARY KEY (`FID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=gbk;
insert into flow values(null,'托方',null);
insert into flow values(null,'xxx',null);
很遗憾,失败了,
ERROR 1366 (HY000): Incorrect string value:ooxx
这是为什么呢?
那我们来看看配置变量
mysql>show variables like '%character%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | E:\Program Files\MySQL\MySQL Server 5.1\share\charsets\ |
+--------------------------+---------------------------------------------------------+
我们可以看见,character_set_client和character_set_connection 都是UTF-8,是这俩的原因吗,我来试了一下
mysql> set character_set_client=gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> source f:/JS/b.sql;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.02 sec)
Query OK, 1 row affected (0.00 sec)
成功了啊。那就这样,说明,你想要插入汉字,只需要把character_set_client和character_set_database 该成一致的就可以了。。。
但是显示起来不爽啊
mysql> select * from flow;
+-----+---------+-------+
| FID | content | stage |
+-----+---------+-------+
| 10 | 鎵樻柟 | NULL |
| 11 | xxx | NULL |
+-----+---------+-------+
2 rows in set (0.00 sec)
那怎么办呢?这么办。。。
mysql> set character_set_results = gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from flow;
+-----+---------+-------+
| FID | content | stage |
+-----+---------+-------+
| 10 | 托方 | NULL |
| 11 | xxx | NULL |
+-----+---------+-------+
2 rows in set (0.00 sec)
成功解决,这只是临时设置,可能一些东西,最好还是写入my.ini中,祝你成功,希望我的经验能帮助你解决问题。。。
阅读(2008) | 评论(0) | 转发(0) |