我使用的是Mysql数据库,数据库的字符集使用UTF-8。
在数据库中显示为中文字段的信息,用otl_stream流输出之后是“问号”。
例如:在表中有一个字段名为subject对应的字段内容是:主题,就是主题,subject from。使用m_cOtlStream >> m_cTmp;输出到m_cTmp变量中,结果变成了:????????,subject from。就是说中文字符变成了问号,英文输入下输出的都是正常的,请问如何解决之?
在数据库中显示为中文字段的信息,用otl_stream流输出之后是“问号”。
例如:在表中有一个字段名为subject对应的字段内容是:主题,就是主题,subject from。使用m_cOtlStream >> m_cTmp;输出到m_cTmp变量中,结果变成了:????????,subject from。就是说中文字符变成了问号,英文输入下输出的都是正常的,请问如何解决之?
输出的m_cTmp变量中变量指定下utf8 试试
http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
MySQL 中文显示乱码
mysql> show variables like 'character_set%';
+--------------------------+----------------------------------------------------
-----+
| Variable_name | Value
|
+--------------------------+----------------------------------------------------
-----+
| character_set_client | latin1
|
| character_set_connection | latin1
|
| character_set_database | latin1
|
| character_set_filesystem | binary
|
| character_set_results | latin1
|
| character_set_server | latin1
|
| character_set_system | utf8
|
| character_sets_dir | D:\Program Files\MySQL\MySQL Server 5.0\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.02 sec)mysql>
declare m_cTmp varchar(100) character set utf8;
你说的是什么意思?m_cTmp我是在VC中定义的char[]变量。
set names latin1
试试
在统一字符集的下重新插入记录
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 5.0.22-community-ntType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mailreceiver |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)mysql> use mailreceiver;
Database changed
mysql> show tables;
+------------------------+
| Tables_in_mailreceiver |
+------------------------+
| mailaccount |
| mailinfo |
| servertable |
| test_tab |
+------------------------+
4 rows in set (0.00 sec)mysql> show create table mailinfo;
+----------+--------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------------------+
| Table | Create Table
|
+----------+--------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------------------+
| mailinfo | CREATE TABLE `mailinfo` (
`Id` int(11) NOT NULL auto_increment,
`Subject` varchar(128) default '',
`From` varchar(64) default NULL,
`To` varchar(64) default NULL,
`DateTime` datetime default '1901-01-01 01:01:01',
`MessageId` varchar(128) default NULL,
`AttachName` varchar(512) default NULL,
`MailPath` varchar(255) default NULL,
`HasRead` bit(1) default '\0' COMMENT '是否阅读',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 |
+----------+--------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------------------+
1 row in set (0.05 sec)mysql> show variables like 'character_set_%';
+--------------------------+----------------------------------------------------
-----+
| Variable_name | Value
|
+--------------------------+----------------------------------------------------
-----+
| character_set_client | gb2312
|
| character_set_connection | gb2312
|
| character_set_database | gb2312
|
| character_set_filesystem | binary
|
| character_set_results | gb2312
|
| character_set_server | gb2312
|
| character_set_system | utf8
|
| character_sets_dir | D:\Program Files\MySQL\MySQL Server 5.0\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.00 sec)mysql> show full columns from mailinfo;
+------------+--------------+-------------------+------+-----+------------------
---+----------------+---------------------------------+----------+
| Field | Type | Collation | Null | Key | Default
| Extra | Privileges | Comment |
+------------+--------------+-------------------+------+-----+------------------
---+----------------+---------------------------------+----------+
| Id | int(11) | NULL | NO | PRI | NULL
| auto_increment | select,insert,update,references | |
| Subject | varchar(128) | gb2312_chinese_ci | YES | | NULL
| | select,insert,update,references | |
| From | varchar(64) | gb2312_chinese_ci | YES | | NULL
| | select,insert,update,references | |
| To | varchar(64) | gb2312_chinese_ci | YES | | NULL
| | select,insert,update,references | |
| DateTime | datetime | NULL | YES | | 1901-01-01 01:01:
01 | | select,insert,update,references | |
| MessageId | varchar(128) | gb2312_chinese_ci | YES | | NULL
| | select,insert,update,references | |
| AttachName | varchar(512) | gb2312_chinese_ci | YES | | NULL
| | select,insert,update,references | |
| MailPath | varchar(255) | gb2312_chinese_ci | YES | | NULL
| | select,insert,update,references | |
| HasRead | bit(1) | NULL | YES | |
| | select,insert,update,references | 是否阅读 |
+------------+--------------+-------------------+------+-----+------------------
---+----------------+---------------------------------+----------+
9 rows in set (0.03 sec)mysql>
---------------------
老大们,再看一下。就是mailinfo里的subject中有中文,在到界面显示时都变成“问号”了。以上是检查的所有字符集设置,有什么问题没有??谢谢。
试试
如果要改成UTF8的话,你需要把数据先导出,然后改表为UTF8后再导入。
我按老大帖子说的重做了一个表,设置如下:
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.22-community-ntType 'help;' or '\h' for help. Type '\c' to clear the buffer.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 | D:\Program Files\MySQL\MySQL Server 5.0\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.00 sec)mysql> select * from test;
ERROR 1046 (3D000): No database selected
mysql> use mailreceiver;
Database changed
mysql> select * from test;
+----+---------------------------------------+
| Id | testchinese |
+----+---------------------------------------+
| 1 | so sosososoos右在夺得地在寺。so yuman |
+----+---------------------------------------+
1 row in set (0.00 sec)mysql> show full columns from test;
+-------------+--------------+----------------+------+-----+---------+----------
------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra
| Privileges | Comment |
+-------------+--------------+----------------+------+-----+---------+----------
------+---------------------------------+---------+
| Id | int(11) | NULL | NO | PRI | NULL | auto_incr
ement | select,insert,update,references | |
| testchinese | varchar(255) | gbk_chinese_ci | YES | | NULL |
| select,insert,update,references | |
+-------------+--------------+----------------+------+-----+---------+----------
------+---------------------------------+---------+
2 rows in set (0.00 sec)mysql> show create table test;
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
| test | CREATE TABLE `test` (
`Id` int(11) NOT NULL auto_increment,
`testchinese` varchar(255) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
1 row in set (0.00 sec)mysql>-------------------------------
然后我在VC中用如下语句去访问
const char* szTestSql = "select * from test";
if (!m_otldb.Open(szTestSql))
{
return;
}
while (!m_otldb.Eof())
{
std::string strOut = m_otldb.FieldbyName("testchinese").c_str();
}得到的strOut的值是:so sosososoos????????so yuman
哪里错了? 为什么得到的中文变问号了。。
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10 to server version: 5.0.22-community-ntType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> show variables like 'character_set_%';
+--------------------------+----------------------------------------------------
-----+
| Variable_name | Value
|
+--------------------------+----------------------------------------------------
-----+
| character_set_client | utf8
|
| character_set_connection | utf8
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | utf8
|
| character_set_server | utf8
|
| character_set_system | utf8
|
| character_sets_dir | D:\Program Files\MySQL\MySQL Server 5.0\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.00 sec)mysql> select * from test;
ERROR 1046 (3D000): No database selected
mysql> use mailreceiver;
Database changed
mysql> select * from test;
+----+-----------------------------------------------+
| Id | testchinese |
+----+-----------------------------------------------+
| 1 | so sosososoos鍙冲湪澶哄緱鍦板湪瀵恒€俿o yuman |
+----+-----------------------------------------------+
1 row in set (0.00 sec)mysql> set names 'gbk';
Query OK, 0 rows affected (0.00 sec)mysql> select * from test;
+----+---------------------------------------+
| Id | testchinese |
+----+---------------------------------------+
| 1 | so sosososoos右在夺得地在寺。so yuman |
+----+---------------------------------------+
1 row in set (0.00 sec)mysql> set names 'gb2312';
Query OK, 0 rows affected (0.00 sec)mysql> select * from test;
+----+---------------------------------------+
| Id | testchinese |
+----+---------------------------------------+
| 1 | so sosososoos右在夺得地在寺。so yuman |
+----+---------------------------------------+
1 row in set (0.00 sec)mysql> set names 'latin1';
Query OK, 0 rows affected (0.00 sec)mysql> select * from test;
+----+-------------------------------+
| Id | testchinese |
+----+-------------------------------+
| 1 | so sosososoos????????so yuman |
+----+-------------------------------+
1 row in set (0.00 sec)mysql> set names 'gbk';
Query OK, 0 rows affected (0.00 sec)mysql> select * from test;
+----+---------------------------------------+
| Id | testchinese |
+----+---------------------------------------+
| 1 | so sosososoos右在夺得地在寺。so yuman |
+----+---------------------------------------+
1 row in set (0.00 sec)mysql>
当设置为'latin1'时查询出来的结果和我看到的结果一样的,问号。什么情况?
你在程序里set names gb2312 取结果试试