标题:VC读取SQL中中文字段问题
只看楼主
cxunmouse
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-5-17
 问题点数:0 回复次数:2 
VC读取SQL中中文字段问题

VC读取SQL中中文字段问题


WinXP,MySql5.0.27,VC++6.0

+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| bk | char(20) | YES | | NULL | |
| num | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
VC++读取下面的记录
+----------+----------+------+
| name | bk | num |
+----------+----------+------+
| a我是神b | c还是神d | 1 |
+----------+----------+------+
显示为
a???b c???d 1
察看内存
97,63,63,63,98
a ,? ,? ,? ,b
不像是乱码问题
具体代码如下:
#include "stdafx.h"

#include <winsock.h>
#include "F:\\MySQL50\\include\\mysql.h"

#pragma comment(lib,"F:\\MySQL50\\lib\\debug\\libmysql.lib")

int main(int argc, char* argv[])
{
MYSQL ms;
MYSQL_RES *mrs;
MYSQL_ROW row;
int res;
int i;

mysql_init(&ms);

if(!mysql_real_connect(&ms,"127.0.0.1","root","huahua","coco",0,NULL,0))
{
printf("Connect Failed!\n");
return 0;
}

res=mysql_query(&ms,"select * from pet");

if(res)
{
printf("mysql_query failed!");
return 0;
}
else
{
mrs=mysql_store_result(&ms);

res=mysql_num_fields(mrs);

while((row=mysql_fetch_row(mrs)))
{
for(i=0;i<res;i++)
{
printf("%s\t",row);
}
printf("\n");
}
mysql_free_result(mrs);
}
mysql_close(&ms);
return 0;
}
在printf("%s\t",row);处设断点,察看row的内存
row[0][0] 97,'a'
row[0][1] 63,'?'
row[0][2] 63,'?'
row[0][3] 63,'?'
row[0][4] 98,'b'
row[1] 'c???d'
row[2] '1'
不知这是什么问题


+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | gb2312 |
| character_set_connection | gb2312 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | gb2312 |
| character_set_server | gb2312 |
| character_set_system | utf8 |
| character_sets_dir | F:\MySQL50\share\charsets\ |
+--------------------------+----------------------------+

搜索更多相关主题的帖子: SQL 字段 中文 
2007-05-17 18:56
lmhllr
Rank: 8Rank: 8
等 级:贵宾
威 望:44
帖 子:1504
专家分:42
注 册:2005-5-12
得分:0 
在查询前执行 set names 'gb2312'  整理输出字符集
2007-05-17 19:54
cxunmouse
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-5-17
得分:0 

谢谢谢
res=mysql_query(&ms,"set names 'gb2312'");
res=mysql_query(&ms,"select * from pet");
这样输出就没问题了

2007-05-17 22:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-140238-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.088924 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved