原有数据库和表都是GBK编码的,我用mysql console 显示中文没有问题,但我使用mysql workbench GUI tools和拿vc程序读都是乱码。它们俩中文乱码是不一样的,workbench GUI 中数据本身没有问题,只是它说是需要UTF8编码,所以就乱码了。而vc程序读中文数据,把中文本身的编码都乱套了,后面全是问号(?)。网上搜集了很多的资料,基本可以确定为字符集的问题。但把字符集设个遍还是搞不定。都搞了好几天了,还是不行,哪位高手帮忙解决下!谢谢!
mysql及数据库中的数据的详细信息如下:mysql> show variables like 'char%';
+--------------------------+----------------------------------------------------
-----+
| Variable_name | Value
|
+--------------------------+----------------------------------------------------
-----+
| character_set_client | gbk
|
| character_set_connection | gbk
|
| character_set_database | gbk
|
| character_set_filesystem | binary
|
| character_set_results | gbk
|
| character_set_server | gbk
|
| character_set_system | utf8
|
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.1\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+mysql> show variables like 'collation%';
+----------------------+----------------+
| Variable_name | Value |
+----------------------+----------------+
| collation_connection | gbk_chinese_ci |
| collation_database | gbk_chinese_ci |
| collation_server | gbk_chinese_ci |
+----------------------+----------------+
3 rows in set (0.00 sec)mysql> show create table beijing;
+---------+---------------------------------------------------------------------
| Table | Create Table
| beijing | CREATE TABLE `beijing` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varbinary(255) NOT NULL DEFAULT '
' COMMENT '????¤??????
??',
`level` tinyint(3) NOT NULL DEFAULT '0',
`parent` varbinary(64) DEFAULT '-1
',
`pcid` int(11) unsigned NOT NULL DEFAULT '0',
`longtitude` int(11) DEFAULT '-1' COMMENT '????(NTU)',
`latitude` int(11) DEFAULT '-1' COMMENT '????(NTU)',
`geo` varchar(100) NOT NULL DEFAULT '',
`admincode` int(11) unsigned NOT NULL DEFAULT '0',
`cid` int(11) unsigned NOT NULL DEFAULT '0',
`next` int(11) unsigned DEFAULT '0' COMMENT '??????????????CID',
`cityid` smallint(6) NOT NULL DEFAULT '0' COMMENT '?????¨°??',
`valid` tinyint(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `CIDIndex` (`cid`),
KEY `NameIndex` (`name`),
KEY `level_index` (`level`),
KEY `admincode_index` (`admincode`),
KEY `pcid_index` (`pcid`),
KEY `parent_index` (`parent`),
KEY `cityid_index` (`cityid`)
) ENGINE=InnoDB AUTO_INCREMENT=346454 DEFAULT CHARSET=gbk COMMENT='????' |
mysql及数据库中的数据的详细信息如下:mysql> show variables like 'char%';
+--------------------------+----------------------------------------------------
-----+
| Variable_name | Value
|
+--------------------------+----------------------------------------------------
-----+
| character_set_client | gbk
|
| character_set_connection | gbk
|
| character_set_database | gbk
|
| character_set_filesystem | binary
|
| character_set_results | gbk
|
| character_set_server | gbk
|
| character_set_system | utf8
|
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.1\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+mysql> show variables like 'collation%';
+----------------------+----------------+
| Variable_name | Value |
+----------------------+----------------+
| collation_connection | gbk_chinese_ci |
| collation_database | gbk_chinese_ci |
| collation_server | gbk_chinese_ci |
+----------------------+----------------+
3 rows in set (0.00 sec)mysql> show create table beijing;
+---------+---------------------------------------------------------------------
| Table | Create Table
| beijing | CREATE TABLE `beijing` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varbinary(255) NOT NULL DEFAULT '
' COMMENT '????¤??????
??',
`level` tinyint(3) NOT NULL DEFAULT '0',
`parent` varbinary(64) DEFAULT '-1
',
`pcid` int(11) unsigned NOT NULL DEFAULT '0',
`longtitude` int(11) DEFAULT '-1' COMMENT '????(NTU)',
`latitude` int(11) DEFAULT '-1' COMMENT '????(NTU)',
`geo` varchar(100) NOT NULL DEFAULT '',
`admincode` int(11) unsigned NOT NULL DEFAULT '0',
`cid` int(11) unsigned NOT NULL DEFAULT '0',
`next` int(11) unsigned DEFAULT '0' COMMENT '??????????????CID',
`cityid` smallint(6) NOT NULL DEFAULT '0' COMMENT '?????¨°??',
`valid` tinyint(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `CIDIndex` (`cid`),
KEY `NameIndex` (`name`),
KEY `level_index` (`level`),
KEY `admincode_index` (`admincode`),
KEY `pcid_index` (`pcid`),
KEY `parent_index` (`parent`),
KEY `cityid_index` (`cityid`)
) ENGINE=InnoDB AUTO_INCREMENT=346454 DEFAULT CHARSET=gbk COMMENT='????' |
不过,那些comment都无所谓了。你在C程序里,获取完连接以后,set names 'gbk'
再获取数据,应该能得到正确的中文显示。前提是数据是在gbk编码下进行插入的。
set names 'gbk'这个我设置了,没有用
试一下
连接时是否指定字符集?表、字段字符集是什么
统一字符集后,重新插入记录试试
数据时之前别人做好了的,我只能看到他建库使用的字符集及表字符集是GBK,表中字段的字符集是默认的
mysql> show create table beijing;
+---------+---------------------------------------------------------------------
| Table | Create Table
| beijing | CREATE TABLE `beijing` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varbinary(255) NOT NULL DEFAULT '
' COMMENT '????¤??????
??',
`level` tinyint(3) NOT NULL DEFAULT '0',
`parent` varbinary(64) DEFAULT '-1
',
`pcid` int(11) unsigned NOT NULL DEFAULT '0',
`longtitude` int(11) DEFAULT '-1' COMMENT '????(NTU)',
`latitude` int(11) DEFAULT '-1' COMMENT '????(NTU)',
`geo` varchar(100) NOT NULL DEFAULT '',
`admincode` int(11) unsigned NOT NULL DEFAULT '0',
`cid` int(11) unsigned NOT NULL DEFAULT '0',
`next` int(11) unsigned DEFAULT '0' COMMENT '??????????????CID',
`cityid` smallint(6) NOT NULL DEFAULT '0' COMMENT '?????¨°??',
`valid` tinyint(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `CIDIndex` (`cid`),
KEY `NameIndex` (`name`),
KEY `level_index` (`level`),
KEY `admincode_index` (`admincode`),
KEY `pcid_index` (`pcid`),
KEY `parent_index` (`parent`),
KEY `cityid_index` (`cityid`)
) ENGINE=InnoDB AUTO_INCREMENT=346454 DEFAULT CHARSET=gbk COMMENT='????' |
我统一都用GBK,也还是不行是不是:| character_set_system | utf8
system 我一直改不了,听网上高手说要改这个值为GBK的,Workbench才能显示,Workbench我不管了,最起码让我VC代码读出来没有问题吧,都是乱码
SET NAMES GBK,
再插入记录,再取出来,看看是否正常
MYSQL提供了5種編程語言2個平臺的接口,
只要你全是用UNICODE,
甚麼語言,甚麼平臺都不會亂碼,
越是設定就越亂碼。
我自己試過到這里下載6個接口,http://www.mysql.com/products/connector/
用6個語言2個平臺對同一個MYSQL寫入讀出都不會有亂碼,
甚麼設定都沒有,信不信由人了。
你说得没错,待会结贴给分,当然这是小事,意思一下
这其中我发现些许不同。
前提:
1。my.in 要设置好字符集
[mysql]
default-character-set=gbk
[mysqld]
...
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gbk
2。插入时设置 set names gbk
3. 显示前要设置set names utf8,否则没法显示,这个我实践了下,不知道为什么?当我字段设成varchar(20)时,workbench能正常显示中文,而当字段设为varbinary(255)时,就不行了,这个不知道怎么回事?
那要是输入文件是unicode,文件没法插入到数据库中
错误如下:
Error Code: 1366
Incorrect integer value: '��1' for column 'id' at row 1)
--------------------------------+
| Table | Create Table
|
+------------+------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------+
| testtable2 | CREATE TABLE `testtable2` (
`id` int(11) DEFAULT NULL,
`owner` varbinary(255) DEFAULT NULL,
`species` varchar(20) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`birth` date DEFAULT NULL,
`death` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+------------+------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------+
1 row in set (0.06 sec)
mysql> select * from testtable2
-> ;
+------+--------+---------+------+------------+------------+
| id | owner | species | sex | birth | death |
+------+--------+---------+------+------------+------------+
| 1 | 王磊 | yellow | f | 1993-02-04 | 1993-02-04 |
| 2 | 冯家刚 | black | m | 1993-02-04 | 1993-02-04 |
| 3 | 洪森 | white | f | 1993-02-04 | 1993-02-04 |
+------+--------+---------+------+------------+------------+
3 rows in set (0.14 sec)workbench显示结果为:
'1', ?, 'yellow', 'f', '1993-02-04', '1993-02-04'
'2', ?, 'black', 'm', '1993-02-04', '1993-02-04'
'3', ?, 'white', 'f', '1993-02-04', '1993-02-04'而如果`owner` varbinary(255) DEFAULT NULL,
变为 `owner` varchar(20) DEFAULT NULL,
则workbench显示结果正常:
1, 王磊, yellow, f, 1993-02-04, 1993-02-04
2, 冯家刚, black, m, 1993-02-04, 1993-02-04
3, 洪森, white, f, 1993-02-04, 1993-02-04
BINARY和VARBINARY类类似于CHAR和VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。
那varbinary 没有字符集概念,那我怎么样使得它在workbench中显示中文不乱码呢?
我现在又一批数据已经建立好了的,其中有两个字段是varbinary(255)类型的,那我怎么才能使得它正常显示呢?乱码的显示结果如下:
id,name,level,parent,pcid
1, �й�, 1, -1, 0, 11645882
2, ������, 2, �й�, 3876633850
3, ����, 2, �й�, 3876633850
4, ������, 4, ������, 3866754442
5, ����, 4, ������, 3866754442
6, ������, 4, ������, 3866754442
7, ����, 4, ������, 3866754442name和parent字段为varbinary(255)字段各位高手,有没有什么办法?
不会只能改成varchar类型把?
SELECT *,UNHEX(HEX(DD)),CONVERT(UNHEX(HEX(DD)) USING gbk) FROM ttv再不行的话,只有转换成 成VARCHAR了