我的工程是unicode工程,字符全是宽字符,可是mysql变量只有CHAR怎么办?

screen12 2016-06-15 10:34:57
我的工程是unicode工程,这样的话,里面所有的字符都是宽字符,即使英文字母,也是占两个字节的宽度的。

可是如果我使用mysql数据库,可是里面的字符串字段,只有CHAR,以及VARCHAR,怎么办?

CHAR是普通的ANSI字符还是宽字符?CHAR(10)占10个字节还是20个字节?

...全文
204 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
笨笨仔 2016-06-20
  • 打赏
  • 举报
回复
在MySQL中直接存、取UNICODE是可以的,只是在MySQL提供的管理软件中看不出来而已,用程序存取效果是一样的,你可以试试呗。
用户 昵称 2016-06-18
  • 打赏
  • 举报
回复
WideCharToMultiByte来转啊。
Co_priest 2016-06-18
  • 打赏
  • 举报
回复
std::string wctoc(wchar_t* wstr)
{
int len= WideCharToMultiByte(CP_ACP,0,wstr,wcslen(wstr),NULL,0,NULL,NULL);
char* str=new char[len+1];
WideCharToMultiByte(CP_ACP,0,wstr,wcslen(wstr),str,len,NULL,NULL);
str[len]='\0';
std::string ret(str);
delete[] str;

return ret;
}

std::wstring ctowc(char* str)
{
int len = MultiByteToWideChar(CP_ACP,0,str,strlen(str),NULL,0);
wchar_t *wstr = new wchar_t[len+1];
MultiByteToWideChar(CP_ACP,0,str,strlen(str),wstr,len);
wstr[len]='\0';

std::wstring ret(wstr);
delete[] wstr;

return ret;
}

std::string string_To_UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);

wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
ZeroMemory(pwBuf, nwLen * 2 + 2);

::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

char * pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);

::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

std::string retStr(pBuf);

delete []pwBuf;
delete []pBuf;

pwBuf = NULL;
pBuf = NULL;

return retStr;
}

std::stringUTF8_To_string(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
memset(pwBuf, 0, nwLen * 2 + 2);

MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

char * pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);

WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

std::string retStr = pBuf;

delete []pBuf;
delete []pwBuf;

pBuf = NULL;
pwBuf = NULL;

return retStr;
}
BeanJoy 2016-06-17
  • 打赏
  • 举报
回复
将mysql的字符集设置为UTF8。 方法很多,网上查。 我们项目建表时一般这样:
CREATE DATABASE netdb CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
screen12 2016-06-16
  • 打赏
  • 举报
回复
引用 3 楼 dustpg 的回复:
转码啊,转成utf-8之类的
这多麻烦啊?能不能直接把unicode字符,也就是宽字符存储在mysql的字段里?
dustpg 2016-06-16
  • 打赏
  • 举报
回复
转码啊,转成utf-8之类的
screen12 2016-06-16
  • 打赏
  • 举报
回复
引用 1 楼 dustpg 的回复:
vc中UNICODE宏只是影响tchar以及带有A/W后缀的WindowsAPI....还是可以用char
但是,我的程序中的字符串,全是宽字符串,包括用户的名字,地址等等。这些字符串要存储到mysql的表里,怎么办?表里只有CHAR型。
叶恭介叶恭介 2016-06-16
  • 打赏
  • 举报
回复
使用VARCHAR,程序代码加上mysql_set_character_set(&m_MySQL, "gbk")),设置命令代码为GBK,当你想执行SQL命令时使用W2A转为多字节,就这样转过去就行了 USES_CONVERSION; char* pQuery = W2A(szQuery);
dustpg 2016-06-16
  • 打赏
  • 举报
回复
那lz的题目应该改成如何在mysql中储存宽字符。我对sql了解不太深, 可以搜啊,http://hily.me/blog/2008/12/mysql-utf8-or-utf16/。 话说sql不是可以存二进制数据么,二进制不就是等于任何数据么
dustpg 2016-06-15
  • 打赏
  • 举报
回复
vc中UNICODE宏只是影响tchar以及带有A/W后缀的WindowsAPI....还是可以用char

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧